繁体   English   中英

如何设置不同的角色并添加新角色

[英]How is set different roles and add new role

我正在使用使用$user->is_admin$user->is_employee$user->is_customer的系统,数据库中没有列is_adminis_employeeis_customer 我知道它来自用户 model。 但是is_adminis_employee没有在任何地方定义。 倾销给了我真假。

我想添加新的检查,例如is_manager 但找不到我可以在哪里添加这个..

调试栏没有显示对is_admin列的任何查询..

它可以位于哪里?

例如我有观察者:

use App\Helper\SearchLog;
use App\User;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class UserObserver
{
    public function roleAttached(User $user, $role, $team)
    {
        if (!$user->is_admin) {
            $type = 'Employee';
            $route = 'admin.employee.edit';

            if ($user->is_customer) {
                $type = 'Customer';
                $route = 'admin.customers.show';
            }

            SearchLog::createSearchEntry($user->id, $type, $user->name, $route);
            SearchLog::createSearchEntry($user->id, $type, $user->email, $route);
        }
    }

如果它不在数据库列中,我不明白它是如何知道is_admin的?

我的用户 model:


namespace App;

use App\Observers\UserObserver;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laratrust\Traits\LaratrustUserTrait;

class User extends Authenticatable
{

    //------------------------------------ Traits ---------------------------

    use LaratrustUserTrait;
    use Notifiable;

    //------------------------------------ Attributes ---------------------------

    protected static function boot() {
        parent::boot();
        static::observe(UserObserver::class);
        static::laratrustObserve(UserObserver::class);

    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $appends = [
        'user_image_url', 'mobile_with_code', 'formatted_mobile'
    ];

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];

    //------------------------------------ Relations ----------------------------

    public function employeeGroup() {
        return $this->belongsTo(EmployeeGroup::class, 'group_id');
    }

    public function todoItems() {
        return $this->hasMany(TodoItem::class);
    }

    public function completedBookings() {
        return $this->hasMany(Booking::class, 'user_id')->where('bookings.status', 'completed');
    }

    public function booking() {
        return $this->belongsToMany(Booking::class);
    }

    public function services() {
        return $this->belongsToMany(BusinessService::class);
    }

    public function leave()
    {
        return $this->hasMany('App\Leave', 'employee_id', 'id');
    }

    public function role()
    {
        return $this->belongsToMany(Role::class);
    }

    public function employeeSchedule()
    {
        return $this->hasMany('App\EmployeeSchedules', 'employee_id', 'id');
    }

    //------------------------------------ Scopes -------------------------------

    public function scopeAllAdministrators() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', 'administrator');
        });
    }

    public function scopeAllCustomers() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', 'customer')->withoutGlobalScopes();
        });
    }

    public function scopeOtherThanCustomers() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', '<>', 'customer');
        });
    }

    public function scopeAllEmployees() {
        return $this->whereHas('roles', function ($query) {
            $query->where('name', 'employee');
        });
    }

    //------------------------------------ Accessors ----------------------------

    public function getUserImageUrlAttribute() {
        if (is_null($this->image)) {
            return asset('img/default-avatar-user.png');
        }
        return asset_url('avatar/' . $this->image);
    }

    public function getRoleAttribute() {
        return $this->roles->first();
    }

    public function getMobileWithCodeAttribute() {
        return substr($this->calling_code, 1).$this->mobile;
    }

    public function getFormattedMobileAttribute() {
        if (!$this->calling_code) {
            return $this->mobile;
        }
        return $this->calling_code.'-'.$this->mobile;
    }

    public function routeNotificationForNexmo($notification) {
        return $this->mobile_with_code;
    }

    public function getIsAdminAttribute() {
        return $this->hasRole('administrator');
    }

    public function getIsEmployeeAttribute() {
        return $this->hasRole('employee');
    }

    public function getIsCustomerAttribute() {
        if ($this->roles()->withoutGlobalScopes()->where('roles.name', 'customer')->count() > 0) {
            return true;
        }
        return false;
    }

    //------------------------------------ Mutators -----------------------------

    public function setPasswordAttribute($value) {
        $this->attributes['password'] = bcrypt($value);
    }

    //------------------------------------ Formats -----------------------------

    public function userBookingCount($date) {
        return Booking::whereNull('deal_id')->where('user_id', $this->id)->whereDate('created_at', $date)->get()->count();
    }

} /* end of class */

LoginController 看起来像这样,其中经过身份验证的 class:

protected function authenticated(Request $request, $user)
    {
        if ($user->is_admin || $user->is_employee) {
            return redirect()->route('admin.dashboard');
        }

        if(!$user->is_admin && !$user->is_employee && Cookie::get('bookingDetails')!==null && Cookie::get('products')!==null && $this->checkUserBooking($user->id)>$this->settings->booking_per_day){
            return redirect(route('front.index'))->withCookie(Cookie::forget('bookingDetails'))->withCookie(Cookie::forget('products'))->withCookie(Cookie::forget('couponData'));
        }
        return redirect(session()->get('url.encoded'));
    }

您可以创建另一个访问器来检查角色是否与当前用户实体相关联。

public function getIsManagerAttribute() {
    return $this->hasRole('manager');// presuming you have created manager role
}

然后你可以很容易地检查

// $user = User::find(1);

// $user->is_manager;// true || false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM