本文整理汇总了PHP中app\User::hasRole方法的典型用法代码示例。如果您正苦于以下问题:PHP User::hasRole方法的具体用法?PHP User::hasRole怎么用?PHP User::hasRole使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\User
的用法示例。
在下文中一共展示了User::hasRole方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: checkRoleInheritance
protected function checkRoleInheritance(User $user = null, $roleName)
{
//未登入直接不通過
if (!$user) {
return false;
}
//直接擁有該角色
if ($user->hasRole($roleName)) {
return true;
}
//檢查角色是否存在
$role = Role::where('name', $roleName)->first();
if (!$role) {
return false;
}
//檢查擁有的角色,是否繼承欲檢查之角色
$roleList = $user->roles;
foreach ($roleList as $roleItem) {
if (isset(static::$inheritance[$roleItem->name]) || array_key_exists($roleItem->name, static::$inheritance)) {
//繼承表有該角色
$roleInheritanceList = static::$inheritance[$roleItem->name];
if (is_array($roleInheritanceList)) {
foreach ($roleInheritanceList as $roleInheritance) {
if ($roleInheritance = $role->name) {
return true;
}
}
}
}
}
return false;
}
示例2: store
public function store(Request $request, $id = null)
{
if (isset($id)) {
if ($request->input('password')) {
$validator = Validator::make($request->only('username', 'email', 'password', 'password_confirmation'), $this->user->update_rules_with_password);
} else {
$validator = Validator::make($request->only('username', 'email', 'password', 'password_confirmation'), $this->user->update_rules);
}
} else {
$validator = Validator::make($request->only('username', 'email', 'password', 'password_confirmation'), $this->user->create_rules);
}
if ($validator->passes()) {
if (isset($id)) {
$user = $this->user->find($id);
} else {
$user = new User();
}
$user->username = $request->input('username');
$user->email = $request->input('email');
if ($request->input('password')) {
$user->password = Hash::make($request->input('password'));
}
$user->save();
$role = Role::where('role_name', 'admin')->first();
if ($request->input('admin')) {
$user->roles()->attach($role);
} else {
if ($user->hasRole('admin')) {
$user->roles()->detach($role);
}
}
return redirect()->to('users')->with(['success' => 'Saved ' . $user->username]);
} else {
return redirect()->back()->withInput()->withErrors($validator->messages());
}
}
示例3: show
public function show(User $user, Group $group)
{
return $user->hasRole('manager') && $user->organization->id === $group->organization->id;
}
示例4: pdf
public function pdf(User $user, Report $report)
{
return $user->hasRole('manager') && $user->id === $report->owner->id;
}
示例5: before
/**
* Determine if user is admin then bypass all checks
*
* @param \App\User $user
* @param \App\Model\Permission $permission
* @return bool
*/
public function before($user, $ability)
{
if ($user->hasRole('admin')) {
return true;
}
}
示例6: destroy
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(User $user, $role_id)
{
if (!Auth::user()->can('administrate-permissions')) {
abort(401, "You can not edit user permissions");
}
$role = Role::where('id', '=', $role_id)->firstOrFail();
if (!$user->hasRole($role->name)) {
abort(403, "User doesn't have the role id of ({$role_id})");
}
$user->removeUserRole($role_id);
return $user;
}
示例7: authenticated
public function authenticated($request, User $user)
{
if ($user->hasRole('customer')) {
$this->redirectTo = '/customer/';
}
return redirect()->intended($this->redirectPath());
}