first_name . ' ' . $user->last_name; // $user->name = $fullName; // //Reset cache // }); } /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'id', 'name', 'email', 'password', 'jwt_token', 'is_active', 'last_name', 'first_name', 'full_name', 'code', 'title', 'department', 'office_phone', 'office_phone', 'mobile_phone', 'show_cid', 'dnd_enabled', 'home_phone', 'fax', 'secondary_email', 'left_panel_hide', 'theme', 'address_country', 'address_state', 'address_city', 'address_postal_code', 'address_street', 'signature', 'time_zone', 'date_format_id', 'time_format_id', 'profile_pic_conf', 'profile_image_path', 'name', 'language_id', 'password', 'access_to_all_entity', 'company_id', 'api_key', 'fcm_token', 'jwt_token', 'hide_from_reports', 'allow_download_recording', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', 'deleted_at' ]; protected $dates = [ 'created_at', 'updated_at' ]; protected $casts = [ 'hide_from_reports' => 'boolean', 'show_cid' => 'boolean', 'dnd_enabled' => 'boolean', ]; /** * Scope that excludes users hidden from reports. * Company managers can see all users; other roles only see non-hidden users, * but the currently authenticated user always sees themselves. */ public function scopeVisibleInReports($query) { $currentUser = \Auth::user(); if ($currentUser && $currentUser->hasRole('company_manager')) { return $query; } $currentUserId = $currentUser ? $currentUser->id : null; return $query->where(function ($q) use ($currentUserId) { $q->where('hide_from_reports', 0); if ($currentUserId) { $q->orWhere('id', $currentUserId); } }); } public function dateFormat() { return $this->belongsTo(DateFormat::class); } public function isAdmin() { return $this->hasPermissionTo('settings.access'); } /** * Whether this user is a company-level admin: they hold the * "Admin Setting" permission (admin.settings) or the "Company Admin" role * (company_manager). * * @return bool */ public function isCompanyAdmin() { return $this->hasPermissionTo('admin.settings') || $this->hasRole(Role::DEFAULT_COMPANY_ADMIN_ROLE); } /** * Whether this user may manage every user, role, group and permission of * their company, including the protected Admin / Company Admin entities. * * True for super admins (settings.access) and for company admins * (Admin Setting permission or Company Admin role). * * @return bool */ public function canManageCompanyAccess() { return $this->isAdmin() || $this->isCompanyAdmin(); } /** * Whether this user may view/manage the given user. * * Super admins (settings.access) may manage anyone. Company admins * (Admin Setting permission or Company Admin role) may manage any user of * their own company, including users holding a protected role. Everyone * else keeps the previous restriction: same company only and never a * protected-role user. * * @param User $target * @return bool */ public function canManageUser(User $target) { if ($this->isAdmin()) { return true; } if ($target->company_id != $this->company_id) { return false; } if (!$this->isCompanyAdmin() && $target->hasProtectedRole()) { return false; } return true; } /** * Whether this user holds a protected role (Admin / Company Admin). * * Protected users cannot be seen or managed by scoped "user.settings" * users that lack full settings access. * * @return bool */ public function hasProtectedRole() { return $this->roles() ->whereIn('name', Role::protectedRoleNames()) ->exists(); } /** * Exclude users that hold a protected role from the query. * * @param \Illuminate\Database\Eloquent\Builder $query * @return \Illuminate\Database\Eloquent\Builder */ public function scopeExcludingProtectedRoles($query) { return $query->whereDoesntHave('roles', function ($q) { $q->whereIn('name', Role::protectedRoleNames()); }); } /** * Verify if user can access company * @param Company $company * @return bool */ public function canAccessCompany(Company $company = null) { if (empty($company)) { $company = \Auth::user()->companyContext(); } if ($this->isAdmin() || $company->id == $this->company->id) { return true; } return false; } public function timeFormat() { return $this->belongsTo(TimeFormat::class); } public function language() { return $this->belongsTo(Language::class); } public function groups() { return $this->belongsToMany(Group::class)->withTimestamps(); } /** * The group (queue) ids this user can access to in call reports. * * Users with access to all entities see every company queue; * anyone else only sees queues they were granted the matching queue * permission for (permission name: {queue_id}.{queue_name}). * * @return array */ public function accessibleQueueIds() { $companyId = $this->company_id ?? 1; if ((string) $this->access_to_all_entity === '1') { return Group::where('company_id', $companyId)->pluck('id')->map(function ($id) { return (int) $id; })->all(); } $grantedQueuePermissions = Permission::where('perm_group', 'groups') ->where('guard_name', 'web') ->get(['id', 'name']) ->filter(function ($permission) { return $this->hasPermissionTo($permission->name); }) ->pluck('name') ->map(function ($name) { return strtolower((string) $name); }) ->all(); return Group::where('company_id', $companyId) ->get(['id', 'name']) ->filter(function (Group $group) use ($grantedQueuePermissions) { return in_array( strtolower($group->id . '.' . strtolower($group->name)), $grantedQueuePermissions, true ); }) ->pluck('id') ->map(function ($id) { return (int) $id; }) ->values() ->all(); } public function theme() { return $this->theme; } public function company() { return $this->belongsTo(Company::class); } public function getJWTIdentifier() { return $this->getKey(); } public function getJWTCustomClaims() { return []; } /** * All SIP/PJSIP/WSS extensions belonging to this user. */ public function extensions() { return $this->hasMany(\Modules\Platform\User\Entities\UserExtension::class, 'user_id'); } public function companyContext() { return session()->get('currentCompany'); } }