First complete upload CasaDoc
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Authorization;
|
||||
|
||||
use Cache;
|
||||
use Config;
|
||||
use Vanguard\Permission;
|
||||
|
||||
trait AuthorizationRoleTrait
|
||||
{
|
||||
/**
|
||||
* Get cached permissions for this role.
|
||||
* @return mixed
|
||||
*/
|
||||
public function cachedPermissions()
|
||||
{
|
||||
return Cache::remember($this->getCacheKey(), Config::get('cache.ttl'), function () {
|
||||
return $this->permissions()->get();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Override "save" role method to clear role cache.
|
||||
* @param array $options
|
||||
*/
|
||||
public function save(array $options = [])
|
||||
{
|
||||
$this->flushCache();
|
||||
parent::save($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override "delete" role method to clear role cache.
|
||||
* @param array $options
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete(array $options = [])
|
||||
{
|
||||
$this->flushCache();
|
||||
parent::delete($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override "restore" role method to clear role cache.
|
||||
*/
|
||||
public function restore()
|
||||
{
|
||||
$this->flushCache();
|
||||
parent::restore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Many-to-Many relations with the permission model.
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
||||
*/
|
||||
public function permissions()
|
||||
{
|
||||
return $this->belongsToMany(Permission::class, 'permission_role', 'role_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the role has a permission by its name.
|
||||
*
|
||||
* @param string $name Permission name.
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPermission($name)
|
||||
{
|
||||
$perms = $this->cachedPermissions()->pluck('name')->toArray();
|
||||
|
||||
return in_array($name, $perms, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the inputted permissions.
|
||||
*
|
||||
* @param mixed $inputPermissions
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function savePermissions($inputPermissions)
|
||||
{
|
||||
if (! empty($inputPermissions)) {
|
||||
$this->permissions()->sync($inputPermissions);
|
||||
} else {
|
||||
$this->permissions()->detach();
|
||||
}
|
||||
|
||||
$this->flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach permission to current role.
|
||||
*
|
||||
* @param object|array $permission
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function attachPermission($permission)
|
||||
{
|
||||
if (is_object($permission)) {
|
||||
$permission = $permission->getKey();
|
||||
}
|
||||
|
||||
if (is_array($permission)) {
|
||||
$permission = $permission['id'];
|
||||
}
|
||||
|
||||
$this->permissions()->attach($permission);
|
||||
|
||||
$this->flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Detach permission from current role.
|
||||
*
|
||||
* @param object|array $permission
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function detachPermission($permission)
|
||||
{
|
||||
if (is_object($permission)) {
|
||||
$permission = $permission->getKey();
|
||||
}
|
||||
|
||||
if (is_array($permission)) {
|
||||
$permission = $permission['id'];
|
||||
}
|
||||
|
||||
$this->permissions()->detach($permission);
|
||||
|
||||
$this->flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach multiple permissions to current role.
|
||||
*
|
||||
* @param mixed $permissions
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function attachPermissions($permissions)
|
||||
{
|
||||
foreach ($permissions as $permission) {
|
||||
$this->attachPermission($permission);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detach multiple permissions from current role
|
||||
*
|
||||
* @param mixed $permissions
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function detachPermissions($permissions)
|
||||
{
|
||||
foreach ($permissions as $permission) {
|
||||
$this->detachPermission($permission);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync role permissions.
|
||||
* @param $permissions array Permission IDs.
|
||||
*/
|
||||
public function syncPermissions(array $permissions)
|
||||
{
|
||||
$this->permissions()->sync($permissions);
|
||||
|
||||
$this->flushCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get permissions cache key.
|
||||
* @return string
|
||||
*/
|
||||
private function getCacheKey()
|
||||
{
|
||||
return 'permissions_for_role_'.$this->{$this->primaryKey};
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush cached permissions for this role.
|
||||
*/
|
||||
private function flushCache()
|
||||
{
|
||||
Cache::forget($this->getCacheKey());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Authorization;
|
||||
|
||||
use Vanguard\Role;
|
||||
|
||||
trait AuthorizationUserTrait
|
||||
{
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function role()
|
||||
{
|
||||
return $this->belongsTo(Role::class, 'role_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has specified role.
|
||||
* @param $role
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRole($role)
|
||||
{
|
||||
return $this->role->name === $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user can perform some action.
|
||||
* @param $permission
|
||||
* @param bool $allRequired
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPermission($permission, $allRequired = true)
|
||||
{
|
||||
$permission = (array) $permission;
|
||||
|
||||
return $allRequired
|
||||
? $this->hasAllPermissions($permission)
|
||||
: $this->hasAtLeastOnePermission($permission);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has all provided permissions
|
||||
* (translates to AND logic between permissions).
|
||||
*
|
||||
* @param array $permissions
|
||||
* @return bool
|
||||
*/
|
||||
private function hasAllPermissions(array $permissions)
|
||||
{
|
||||
$availablePermissions = $this->role->cachedPermissions()->pluck('name')->toArray();
|
||||
|
||||
foreach ($permissions as $perm) {
|
||||
if (! in_array($perm, $availablePermissions, true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user has at least one of provided permissions
|
||||
* (translates to OR logic between permissions).
|
||||
*
|
||||
* @param array $permissions
|
||||
* @return bool
|
||||
*/
|
||||
private function hasAtLeastOnePermission(array $permissions)
|
||||
{
|
||||
$availablePermissions = $this->role->cachedPermissions()->pluck('name')->toArray();
|
||||
|
||||
foreach ($permissions as $perm) {
|
||||
if (in_array($perm, $availablePermissions, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user's role.
|
||||
* @param Role $role
|
||||
* @return mixed
|
||||
*/
|
||||
public function setRole($role)
|
||||
{
|
||||
return $this->forceFill([
|
||||
'role_id' => $role instanceof Role ? $role->id : $role
|
||||
])->save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support;
|
||||
|
||||
trait CanImpersonateUsers
|
||||
{
|
||||
/**
|
||||
* Check if a user can impersonate other users.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canImpersonate()
|
||||
{
|
||||
return $this->hasPermission('users.manage');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a target user can be impersonated.
|
||||
* By default, all users can be impersonated if a currently logged
|
||||
* user is not already impersonating another user.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canBeImpersonated()
|
||||
{
|
||||
return ! app('impersonate')->isImpersonating();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Enum;
|
||||
|
||||
class UserStatus
|
||||
{
|
||||
const UNCONFIRMED = 'Unconfirmed';
|
||||
const ACTIVE = 'Active';
|
||||
const BANNED = 'Banned';
|
||||
|
||||
public static function lists()
|
||||
{
|
||||
return [
|
||||
self::ACTIVE => trans('app.status.'.self::ACTIVE),
|
||||
self::BANNED => trans('app.status.'. self::BANNED),
|
||||
self::UNCONFIRMED => trans('app.status.' . self::UNCONFIRMED)
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard;
|
||||
|
||||
use Vanguard\Plugins\Plugin;
|
||||
use Vanguard\Support\Sidebar\Item;
|
||||
|
||||
class Dashboard extends Plugin
|
||||
{
|
||||
public function sidebar()
|
||||
{
|
||||
return Item::create(__('Dashboard'))
|
||||
->route('dashboard')
|
||||
->icon('fas fa-home')
|
||||
->active("/");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard\Widgets;
|
||||
|
||||
use Vanguard\Plugins\Widget;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Support\Enum\UserStatus;
|
||||
|
||||
class BannedUsers extends Widget
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $width = '3';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $permissions = 'users.manage';
|
||||
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
protected $users;
|
||||
|
||||
/**
|
||||
* BannedUsers constructor.
|
||||
* @param UserRepository $users
|
||||
*/
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.banned-users', [
|
||||
'count' => $this->users->countByStatus(UserStatus::BANNED)
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard\Widgets;
|
||||
|
||||
use Vanguard\Plugins\Widget;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
|
||||
class LatestRegistrations extends Widget
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $width = '4';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $permissions = 'users.manage';
|
||||
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $users;
|
||||
|
||||
/**
|
||||
* LatestRegistrations constructor.
|
||||
* @param UserRepository $users
|
||||
*/
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.latest-registrations', [
|
||||
'latestRegistrations' => $this->users->latest(6)
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard\Widgets;
|
||||
|
||||
use Vanguard\Plugins\Widget;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
|
||||
class NewUsers extends Widget
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $width = '3';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $permissions = 'users.manage';
|
||||
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $users;
|
||||
|
||||
/**
|
||||
* NewUsers constructor.
|
||||
* @param UserRepository $users
|
||||
*/
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.new-users', [
|
||||
'count' => $this->users->newUsersCount()
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard\Widgets;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Vanguard\Plugins\Widget;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
|
||||
class RegistrationHistory extends Widget
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $width = '8';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $permissions = 'users.manage';
|
||||
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $users;
|
||||
|
||||
/**
|
||||
* @var array Count of new users per month.
|
||||
*/
|
||||
protected $usersPerMonth;
|
||||
|
||||
/**
|
||||
* RegistrationHistory constructor.
|
||||
* @param UserRepository $users
|
||||
*/
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.registration-history', [
|
||||
'usersPerMonth' => $this->getUsersPerMonth()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function scripts()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.registration-history-scripts', [
|
||||
'usersPerMonth' => $this->getUsersPerMonth()
|
||||
]);
|
||||
}
|
||||
|
||||
private function getUsersPerMonth()
|
||||
{
|
||||
if ($this->usersPerMonth) {
|
||||
return $this->usersPerMonth;
|
||||
}
|
||||
|
||||
return $this->usersPerMonth = $this->users->countOfNewUsersPerMonth(
|
||||
Carbon::now()->subYear()->startOfMonth(),
|
||||
Carbon::now()->endOfMonth()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard\Widgets;
|
||||
|
||||
use Vanguard\Plugins\Widget;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
|
||||
class TotalUsers extends Widget
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $width = '3';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $permissions = 'users.manage';
|
||||
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $users;
|
||||
|
||||
/**
|
||||
* TotalUsers constructor.
|
||||
* @param UserRepository $users
|
||||
*/
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.total-users', [
|
||||
'count' => $this->users->count()
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard\Widgets;
|
||||
|
||||
use Vanguard\Plugins\Widget;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\Support\Enum\UserStatus;
|
||||
|
||||
class UnconfirmedUsers extends Widget
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public $width = '3';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $permissions = 'users.manage';
|
||||
|
||||
/**
|
||||
* @var UserRepository
|
||||
*/
|
||||
private $users;
|
||||
|
||||
/**
|
||||
* UnconfirmedUsers constructor.
|
||||
* @param UserRepository $users
|
||||
*/
|
||||
public function __construct(UserRepository $users)
|
||||
{
|
||||
$this->users = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.unconfirmed-users', [
|
||||
'count' => $this->users->countByStatus(UserStatus::UNCONFIRMED)
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins\Dashboard\Widgets;
|
||||
|
||||
use Vanguard\Plugins\Widget;
|
||||
use Vanguard\User;
|
||||
|
||||
class UserActions extends Widget
|
||||
{
|
||||
/**
|
||||
* UserActions constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->permissions(function (User $user) {
|
||||
return $user->hasRole('User');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return view('plugins.dashboard.widgets.user-actions');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins;
|
||||
|
||||
use Vanguard\Plugins\Plugin;
|
||||
use Vanguard\Support\Sidebar\Item;
|
||||
|
||||
class RolesAndPermissions extends Plugin
|
||||
{
|
||||
public function sidebar()
|
||||
{
|
||||
$roles = Item::create(__('Roles'))
|
||||
->route('roles.index')
|
||||
->active("roles*")
|
||||
->permissions('roles.manage');
|
||||
|
||||
$permissions = Item::create(__('Permissions'))
|
||||
->route('permissions.index')
|
||||
->active("permissions*")
|
||||
->permissions('permissions.manage');
|
||||
|
||||
return Item::create(__('Roles & Permissions'))
|
||||
->href('#roles-dropdown')
|
||||
->icon('fas fa-users-cog')
|
||||
->permissions(['roles.manage', 'permissions.manage'])
|
||||
->addChildren([
|
||||
$roles,
|
||||
$permissions
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins;
|
||||
|
||||
use Vanguard\Plugins\Plugin;
|
||||
use Vanguard\Support\Sidebar\Item;
|
||||
use Vanguard\User;
|
||||
|
||||
class Settings extends Plugin
|
||||
{
|
||||
public function sidebar()
|
||||
{
|
||||
$general = Item::create(__('General'))
|
||||
->route('settings.general')
|
||||
->active("settings")
|
||||
->permissions('settings.general');
|
||||
|
||||
$authAndRegistration = Item::create(__('Auth & Registration'))
|
||||
->route('settings.auth')
|
||||
->active("settings/auth")
|
||||
->permissions('settings.auth');
|
||||
|
||||
$notifications = Item::create(__('Notifications'))
|
||||
->route('settings.notifications')
|
||||
->active("settings/notifications")
|
||||
->permissions(function (User $user) {
|
||||
return $user->hasPermission('settings.notifications');
|
||||
});
|
||||
|
||||
return Item::create(__('Settings'))
|
||||
->href('#settings-dropdown')
|
||||
->icon('fas fa-cogs')
|
||||
->permissions(function (User $user) {
|
||||
return $user->hasPermission(
|
||||
['settings.general', 'settings.auth', 'settings.notifications'],
|
||||
allRequired: false
|
||||
);
|
||||
})
|
||||
->addChildren([
|
||||
$general,
|
||||
$authAndRegistration,
|
||||
$notifications,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Plugins;
|
||||
|
||||
use Vanguard\Plugins\Plugin;
|
||||
use Vanguard\Support\Sidebar\Item;
|
||||
|
||||
class Users extends Plugin
|
||||
{
|
||||
public function sidebar()
|
||||
{
|
||||
return Item::create(__('Users'))
|
||||
->route('users.index')
|
||||
->icon('fas fa-users')
|
||||
->active("users*")
|
||||
->permissions('users.manage');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Support\Sidebar;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Collection;
|
||||
use Vanguard\User;
|
||||
|
||||
class Item
|
||||
{
|
||||
protected $title;
|
||||
protected $route;
|
||||
protected $href;
|
||||
protected $icon;
|
||||
protected $activePath;
|
||||
protected $permissions;
|
||||
protected $children;
|
||||
|
||||
/**
|
||||
* Item constructor.
|
||||
* @param $title
|
||||
*/
|
||||
public function __construct($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to easily create a new Item instance
|
||||
* with a given title.
|
||||
*
|
||||
* @param $title
|
||||
* @return Item
|
||||
*/
|
||||
public static function create($title)
|
||||
{
|
||||
return new self($title);
|
||||
}
|
||||
|
||||
/**
|
||||
* The route to which the rendered item
|
||||
* should point to.
|
||||
*
|
||||
* @param $route
|
||||
* @return $this
|
||||
*/
|
||||
public function route($route)
|
||||
{
|
||||
$this->route = $route;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* A URL to be used if there is no named route
|
||||
* defined for the navigation item or if it is an
|
||||
* external URL.
|
||||
*
|
||||
* If this attribute is set it will have higher
|
||||
* priority than the $route attribute.
|
||||
*
|
||||
* @param $href
|
||||
* @return $this
|
||||
*/
|
||||
public function href($href)
|
||||
{
|
||||
$this->href = $href;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sidebar navigation icon.
|
||||
*
|
||||
* @param $icon
|
||||
* @return $this
|
||||
*/
|
||||
public function icon($icon)
|
||||
{
|
||||
$this->icon = $icon;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The path which indicates when this navigation
|
||||
* item should be marked as active. It can contain
|
||||
* wildcard characters.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* 'users*' (the item will be active whenever a current URL start with "user")
|
||||
*
|
||||
* @param $path
|
||||
* @return $this
|
||||
*/
|
||||
public function active($path)
|
||||
{
|
||||
$this->activePath = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Active path getter.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getActivePath()
|
||||
{
|
||||
return $this->activePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* If item has secondary navigation links, this
|
||||
* method will return all the URL patterns when
|
||||
* this navigation item should be expanded.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function getExpandedPath()
|
||||
{
|
||||
if (! $this->children->count()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->children->toBase()->map(function (Item $item) {
|
||||
return $item->getActivePath();
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the "href" attribute (the URL) for the navigation item.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHref()
|
||||
{
|
||||
if ($this->href) {
|
||||
return $this->href;
|
||||
}
|
||||
|
||||
return $this->route ? route($this->route) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Icon getter.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getIcon()
|
||||
{
|
||||
return $this->icon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Title getter.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the permissions required for rendering the
|
||||
* navigation item.
|
||||
*
|
||||
* @param string|array $permissions
|
||||
* @return $this
|
||||
*/
|
||||
public function permissions($permissions)
|
||||
{
|
||||
$this->permissions = $permissions;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permissions getter.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPermissions()
|
||||
{
|
||||
return $this->permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if item has nested "children" items.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDropdown()
|
||||
{
|
||||
return $this->children && $this->children->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the collection of nested items.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function children()
|
||||
{
|
||||
return $this->children;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach an array of children to the item.
|
||||
*
|
||||
* @param array $children
|
||||
* @return $this
|
||||
*/
|
||||
public function addChildren(array $children)
|
||||
{
|
||||
if (is_null($this->children)) {
|
||||
$this->children = new Collection;
|
||||
}
|
||||
|
||||
foreach ($children as $child) {
|
||||
$this->children->push($child);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the specified user can view the item.
|
||||
*
|
||||
* @param User $user
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public function authorize(User $user)
|
||||
{
|
||||
if (is_object($this->permissions) && $this->permissions instanceof Closure) {
|
||||
return call_user_func($this->permissions, $user);
|
||||
}
|
||||
|
||||
foreach ((array) $this->permissions as $permission) {
|
||||
if (! $user->hasPermission($permission)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user