/** * Finds user's roles. * * @param User $user * @return Role[] */ public static function findRoles(User $user) { static $cached = []; if ($ids = array_diff($user->roles, array_keys($cached))) { $cached += Role::where('id IN (' . implode(',', $user->roles) . ')')->get(); } return array_intersect_key($cached, array_flip($user->roles)); }
/** * Gets the user roles. * * @param User $user * @return array */ protected function getRoles(User $user = null) { $roles = []; $self = $user && $user->id === App::user()->id; foreach (Role::where(['id <> ?'], [Role::ROLE_ANONYMOUS])->orderBy('priority')->get() as $role) { $r = $role->jsonSerialize(); if ($role->isAuthenticated()) { $r['disabled'] = true; } if ($user && $role->isAdministrator() && (!App::user()->isAdministrator() || $self)) { $r['disabled'] = true; } $roles[$r['id']] = $r; } return $roles; }