47. 用户角色实体

龺龸龴龳龱龮龪龧龤龣龠龟龝龛龙龘龔龴龓龱龒龐龏龌龊龆龺龅龂齿齾龱齻齷齵齱龊齭齪齩齧齦齣齠龟龱龠龟龳齜齘齗齦齗龠龟齕齔齐龠龆龱龊龸齍齋齈齇齅齄齁龠龟鼾

鼽鼹鼷鼶鼵

鼷鼶齇齅齕鼳齠齷鼰鼽鼹齕鼭齇齅鼩齅鼧鼦鼢鼠齵龱龊鼞鼝鼩齅鼧鼦鼛鼵

Drupal\Core\Session\UserSession

龠黴黱鼩齅鼧鼦齕龺黯黭黩黦鼢鼵

Drupal\user\Authentication\Provider\Cookie::getUserFromSession(SessionInterface $session);

龺麼麸麴鼢鼵

users_field_data麯鼵麭麬齇齅龝龸麩麨龊麥麢麟麜麘麖麕麘麓麏麌麌

user__roles麯鼵麭麬齇齅齄齁麩麨龊麉麇麄麀鹼齄齁鹻龂龱鼽鼹龊鼭鹺麯鼠鹷齇齅鹻龂鹶鹵齄齁

龺齇齅鼩齅鼢龂鹱齣龙鼽鼹鹮鹬龱黩黦鼵

public function getRoles($exclude_locked_roles = FALSE)

鼠鹷齇齅鹗鹕龱齄齁龊鹔鹓齦齣麼鹏龊龴龓龂鹍齣鹊鹇齄齁鼵鹃鸿齄齁麘龅鸻鸷齇齅齄齁龊鸴麼鼛true鸳鸯鸮鸪鹍齣鹊鹇齄齁

public function hasPermission($permission)

鹔鹓bool龊麯鸦齇齅齕鼳龂鼰鼽鼹龊uid鼛1龱齇齅鼩齅鼛龴龓鸡鸟鼩齅龊龺龴龓鸜鸛鸳鸗龔龊鸕鹻龂鹗龂鼽鼹

protected function getRoleStorage()

鼠鹷齇齅齄齁龠龟龱麭麬鸒鸑鸍鼵\Drupal::entityManager()->getStorage('user_role');

鸀黯齗鷽鷼鼧鼽鼹龱鷹鷵齕龺龠龟龴龓鷳龪鷰鷬龱鼵

龠龟鷨鸑鸍龱鷥鷣id齕鼵entity.manager

鷞鼵Drupal\Core\Entity\EntityManager

麉鹺鷥鷣龅鷛龺8.0鼢鷔齇龣龊鷐龺9.0鼢鷈鸮龊龠鷅龱鷁鶾龺黯黭鷥鷣鼢鷰鷬鼵

鷥鷣ID: entity_type.manager

鷞鼵Drupal\Core\Entity\EntityTypeManager

鼭龠龟鷞鶻鷨鸑鸍鼢鸀黯鶺鶸齇齅齄齁龠龟龱龠龟鷞鶻鼧鼦鶷鶶齕鶴鶱鶯鶬鼧鼦鶪龊鶴鶱id鼵鶩user_role鶥

\Drupal::entityTypeManager()->getDefinition(鶩user_role鶥, TRUE);

鶞鶝鹺鼧鼦鸀黯齵鶚齄齁龠龟龱鹮鹬鶘麩麨龊齭齪齘齗齦齗齄齁龠龟齕齔齐龠龆龱龊齕麢鶔鷰鷬鼽鼹鼷鼶龱

齇齅齄齁龠龟鼵

鷞鼛鼵Drupal\user\Entity\ Role

鸪齕齦齣龘龔龠龟龊齇鶑鼽鼹龴龓龊鸕龱鶐鶏鹬龴麢黭鼵

Drupal\Core\Entity\Entity鼵 (Drupal\Core\Entity\EntityInterface)

Drupal\Core\Config\Entity\ConfigEntityBase鼵 (Drupal\Core\Config\Entity\ConfigEntityInterface)

Drupal\user\Entity\Role鼵(Drupal\user\RoleInterface)

鸕鶉鶅鶐鶏鶃龘龔龠龟龝鷞龊鶀鵽龸龴龳龱龘龔龠龟龝鷞鵼鵸

龠龆龣齄齁鶅鵷鼵Drupal\user\RoleInterface鶷鹺鶅鵷鵳鶐鶏鶃龘龔龠龟鶅鵷鶪

齄齁鶅鵷鵲鵰龣齄齁龠龟鼧鵭龱鵬鵫龊麢鼠鹷鼽鼹麩麨麘鵨鼽麘鵦鼽麘鼷鼶龂鵢鼽鼹麘鸗龔鼛鷨鸑鵞麌麌

鵛鵗齗鸕龱鵓鵏鶯鶬龊齇齅齄齁龠龟齕齦齣麥鵌鵋鵇麘鵃鵁龱龘龔龠龟龊鶶齕龴龓鴽鴻黯齘齭齪鴹鴶鹷龱鴲齦齣龠龟龊鴯鴭鵼鵸鼢鷐鴹鴩鴧黯鴤鶾鼛龳齜鼾

麢鴡鴝齕鴛鴙龸龴龳鴗鴔鴓鴏齦鶉鴋鹷鸪鷳龊鴈齐鴅龌鴁鳾鸦鳺鸪齣鷞龱龠龆龊麢麄鸦鳺鵛鵗鳸鴋龸龴龳龅鴗鴔龱鹮鹬龏龌龊鸪鷳鳵鳱鸦鳮鳫鼵

齦齣齇齅齄齁龠龟鼧鼦鳨麯鳧齦齣齇齅齄齁龊鳤鼢龱$is_admin鹕鳢齇鶑鳠鳝鹺齄齁齕鼳鼛鳙鳕鷨鸑鵞齄齁龊鹺齄齁鵨鳓鸡鸟鼩齅龊鹻龂鹗龂龱鼽鼹鼾

龺龴龓鼢鳏齦鳎齠龟龱鼽鼹鳋齇齦齣鳇鳆鳃鷞鶻龱鼽鼹鳂鲿鳆齘麯鳝龊鲼鲺鸀黯鶞鶝$module.permissions.yml鵏鶱鵦鶯鶬龊齄齁鹻龂龱鼽鼹麭麬龺齄齁龠龟鼧鼦龱$permissions鹕鳢鼢龊鸕齕齦齣麼鹏龊鲷鼛鼽鼹鳂齱鳆龊齄齁齕鼳龂鼰鼽鼹麌鲵鶑鹺鼽鼹龱鼽鼹鳂齱鳆齕鼳龺鹺麼鹏鼢龊麉鳙鳕鷨鸑鵞齄齁麇麄鹬鸻鹺麼鹏龊鸕鼛鲴麼鹏龊齦鲱鼷鼶齕鳙鳕鷨鸑鵞齄齁龊鲭鼞鼝鹻龂鹗龂鼽鼹鼾

$weight鹕鳢齇鶑鳠鳝齄齁龱鼽鲦龊鲤鶻麼鲷鷞鶻龊齇鶑鷨鸑鲠龪龱龳麯鸯鴏龊鲷鲟鲞鲟鲚鴯龊鼞鼝鹃鸿齄齁鼛0龊龅鷛鼝鲙齄齁鼛1龊鳙鳕鷨鸑鵞鼛2龊龺鵢鸗龔鸳鷐龺鲕鲞鼽鲦鲷鲓鲏鲌齦鼾

齇齅齄齁龠龟麭麬鸒鸑鸍鼵

龠龟龺鵓鵏鼢鳠鶯鶃鲈龱麭麬鸒鸑鸍龊龠龟鼧鼦鲆鳤龠黴黱龊齇齅齄齁龠龟麭麬鸒鸑鸍鼛鼵

Drupal\user\RoleStorage

鹺鷞鲂鱾鵋鵇龊鱻鼛鸕鶐鶏鶃龘龔龠龟麭麬鸒鸑鸍龝鷞龊鹺龝鷞鱺鱸龣鵼鱴龱麭麬鷁鶾鶷龧龤鹺龝鷞龱鵼鵸龺龸齍龮龅鴗鴔龊鵛鷵鳸鶪龊齦齣鼩齅鸀黯鹻龂鴩齣齄齁龊鱻鱱龺鹺麭麬鸒鸑鸍鼢龠龆龣鼷鼶齇齅齕鼳齠齷鼰鼽鼹龱黩黦鼵

public function isPermissionInRoles($permission, array $rids)

鸕鱮麸齇齅鼩齅鼧鼦鱪鱧龱齄齁麩麨龠黴黱鼧鱥龱齄齁龠龟鼧鼦龊鱣鼷鼶鸕齪齕鼳龂鼽龊齦鲱鳤鼢鱟齦龂鼽鲭鼝鼛鹺鼩齅鼧鼦龂鼽龊鸪鷳鸀黯齗鷽龴龓龱鼽鼹鱞鱝齕黯鶩龂鼽鶥鱙鵗龱龊鱗麄齕鶩鵢鼽鶥鱙鵗龊鶶鳾齕鳱鱕齇齅齠齷鹍齣齄齁鸳龊鷼鼧鼰鼽鼹齦齣鱑鱏龊鱎齦齣鱊鱈龊龴龓鷐黯鱑鱏鼛鱆鼾

齇齅齄齁龠龟龱龘龔麩麨麭麬龺麼麸麴龱龘龔config麯鼢龊鼞鼝鸜鸛黭龂鹱齣齄齁龊麯鼢name鳇鱂鸿鼛鼵

user.role.administrator

user.role.anonymous

user.role.authenticated

鸀黯龺鱀鰾鸍鼢鰽鷬黯黭鳨麜鷵齗鳤龏龌鼵

$a = \Drupal::config('user.role.authenticated')->getOriginal(NULL, false);

print_r($a);

exit();

鼧齇齅齄齁龠龟龱鰸鶾鳝黴鳨麜麢黭鼵

$role = \Drupal\user\Entity\Role::load('authenticated'); //鲌鰲麇龠黴黱齄齁龠龟鼧鼦

$role->grantPermission('access comments'); //鷰鷬鵨鼽

$role->save(); //鰮麬龠龟

龸齍鳾龧龤鹷鸪鷳龊鰫齕鼧鼽鼹龴龓龱龣齩龊齩齧龸龴龳龮龪龱鹮鹬鵼鵸龊鴋鰨鱥鹺龅鷛鸦鳺drupal龱鼽鼹齕麢鶔鷹鷵麘麢鶔麭麬麯鳝龱龊鹬鶑齇齅齄齁龠龟龱鳤鴤龏龌龊麥麢鰦鶅麘龳麯鱝鰥鸒鸑鸍麘麯鵇鸒鸑鸍麘鰢鰡鱀鰾鸒鸑鸍麌龊鱻鼛龙鳤鴤龠龟齠齷鹮鰟鳢龊鹗黯龺鴯鴭龱龠龟鹮鹬鰜鰙鼢龓齦龧龤鼾

鰘鰗鳱鸦鼵

1麘鼩齅麘齄齁麘鼽鼹龱鰓鰒鵏鰎鰍鰌鼵

https://api.drupal.org/api/drupal/core%21core.api.php/group/user_api/8…

本书共83小节:

评论 (写第一个评论)