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…

本书共63小节:

评论 (写第一个评论)