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…

本书共71小节:

评论 (写第一个评论)