Check whether the user has a certain permission based on their groups.
public hasPermission ( string $permission ) : boolean | ||
$permission | string | |
Результат | boolean |
/** * @param User $actor * @param Builder $query */ public function find(User $actor, Builder $query) { if (!$actor->hasPermission('viewDiscussions')) { $query->whereRaw('FALSE'); } elseif (!$actor->hasPermission('discussion.hide')) { $query->where(function ($query) use($actor) { $query->whereNull('discussions.hide_time')->where('comments_count', '>', 0)->orWhere('start_user_id', $actor->id); $this->events->fire(new ScopeHiddenDiscussionVisibility($query, $actor, 'discussion.hide')); }); } }
/** * @param User $actor * @param string $ability * @param Discussion $discussion * @return bool */ public function before(User $actor, $ability, Discussion $discussion) { // Wrap all discussion permission checks with some logic pertaining to // the discussion's tags. If the discussion has a tag that has been // restricted, and the user has this permission for that tag, then they // are allowed. If the discussion only has tags that have been // restricted, then the user *must* have permission for at least one of // them. $tags = $discussion->tags; if (count($tags)) { $restricted = true; foreach ($tags as $tag) { if ($tag->is_restricted) { if ($actor->hasPermission('tag' . $tag->id . '.discussion.' . $ability)) { return true; } } else { $restricted = false; } } if ($restricted) { return false; } } }
/** * @param User $actor * @param string $ability * @return bool|null */ public function before(User $actor, $ability) { if ($actor->hasPermission('user.' . $ability)) { return true; } }
/** * @param User $actor * @param Tag $tag * @return bool|null */ public function startDiscussion(User $actor, Tag $tag) { if (!$tag->is_restricted && $actor->hasPermission('startDiscussion') || $actor->hasPermission('tag' . $tag->id . '.startDiscussion')) { return true; } }
/** * @param User $user * @param string $permission * @param bool $condition * @return array */ protected static function getIdsWherePermission(User $user, $permission, $condition = true) { static $tags; if (!$tags) { $tags = static::with('parent')->get(); } $ids = []; $hasGlobalPermission = $user->hasPermission($permission); $canForTag = function (Tag $tag) use($user, $permission, $hasGlobalPermission) { return $hasGlobalPermission && !$tag->is_restricted || $tag->is_restricted && $user->hasPermission('tag' . $tag->id . '.' . $permission); }; foreach ($tags as $tag) { $can = $canForTag($tag); if ($can && $tag->parent) { $can = $canForTag($tag->parent); } if ($can === $condition) { $ids[] = $tag->id; } } return $ids; }
/** * @param User $actor * @param string $ability * @return bool|null */ public function after(User $actor, $ability) { if ($actor->hasPermission('group.' . $ability)) { return true; } }