芝麻web文件管理V1.00
编辑当前文件:/home/qrafawbu/qrafiqxcreativeagency.com/accounts/office/app/Models/User.php
$timeLogs * @property-read int|null $time_logs_count * @property-read Collection
$visa * @property-read int|null $visa_count * @method static Builder|User onlyEmployee() * @method static Builder|User wherePmLastFour($value) * @method static Builder|User wherePmType($value) * @method static Builder|User whereStripeId($value) * @method static Builder|User whereTelegramUserId($value) * @method static Builder|User whereTrialEndsAt($value) * @property int|null $country_phonecode * @property-read Collection
$agentGroup * @property-read int|null $agent_group_count * @property-read mixed $mobile_with_phone_code * @method static Builder|User whereCountryPhonecode($value) * @mixin \Eloquent */ class User extends BaseModel { use Notifiable, EntrustUserTrait, HasFactory, TwoFactorAuthenticatable; use HasCompany; use HasMaskImage; const ALL_ADDED_BOTH = ['all', 'added', 'both']; public static function boot() { parent::boot(); static::addGlobalScope(new ActiveScope()); } protected $with = ['clientDetails', 'employeeDetail', 'leaves']; /** * The attributes that are mass assignable. * * @var array */ protected $guarded = [ 'id' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'created_at', 'updated_at', ]; public $dates = ['created_at', 'updated_at', 'last_login']; protected $casts = [ 'created_at' => 'datetime', 'updated_at' => 'datetime', 'last_login' => 'datetime', 'two_factor_expires_at ' => 'array', 'salutation' => Salutation::class, ]; protected $appends = ['image_url', 'modules', 'mobile_with_phonecode']; public function getImageUrlAttribute() { $gravatarHash = !is_null($this->email) ? md5(strtolower(trim($this->email))) : md5($this->id); return ($this->image) ? asset_url_local_s3('avatar/' . $this->image) : 'https://www.gravatar.com/avatar/' . $gravatarHash . '.png?s=200&d=mp'; } public function maskedImageUrl(): Attribute { return Attribute::make( get: function () { return ($this->image) ? $this->generateMaskedImageAppUrl('avatar/' . $this->image) : 'https://www.gravatar.com/avatar/' . md5($this->id) . '.png?s=200&d=mp'; }, ); } public function hasGravatar($email) { // Craft a potential url and test its headers $hash = md5(strtolower(trim($email))); $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404'; $headers = @get_headers($uri); $has_valid_avatar = true; try { if (!preg_match('|200|', $headers[0])) { $has_valid_avatar = false; } } catch (\Exception $e) { $has_valid_avatar = true; } return $has_valid_avatar; } public function getMobileWithPhoneCodeAttribute() { if (!is_null($this->mobile) && !is_null($this->country_phonecode)) { return '+' . $this->country_phonecode . $this->mobile; } return '--'; } /** * Route notifications for the Slack channel. * * @return string */ public function routeNotificationForSlack() { $slack = $this->company->slackSetting; return $slack->slack_webhook; } public function routeNotificationForOneSignal() { return $this->onesignal_player_id; } public function routeNotificationForTwilio() { if (!is_null($this->mobile) && !is_null($this->country_phonecode)) { return '+' . $this->country_phonecode . $this->mobile; } return null; } // phpcs:ignore public function routeNotificationForEmail($notification = null) { $containsExample = Str::contains($this->email, 'example'); if (\config('app.env') === 'demo' && $containsExample) { return null; } return $this->email; } // phpcs:ignore public function routeNotificationForNexmo($notification) { if (!is_null($this->mobile) && !is_null($this->country_phonecode)) { return $this->country_phonecode . $this->mobile; } return null; } // phpcs:ignore public function routeNotificationForVonage($notification) { if (!is_null($this->mobile) && !is_null($this->country_phonecode)) { return $this->country_phonecode . $this->mobile; } return null; } // phpcs:ignore public function routeNotificationForMsg91($notification) { if (!is_null($this->mobile) && !is_null($this->country_phonecode)) { return $this->country_phonecode . $this->mobile; } return null; } public function clientDetails(): HasOne { return $this->hasOne(ClientDetails::class, 'user_id'); } public function userAuth(): BelongsTo { return $this->belongsTo(UserAuth::class, 'user_auth_id'); } public function lead(): HasOne { return $this->hasOne(Lead::class, 'user_id'); } public function attendance(): HasMany { return $this->hasMany(Attendance::class, 'user_id'); } public function employee(): HasMany { return $this->hasMany(EmployeeDetails::class, 'user_id'); } public function employeeDetail(): HasOne { return $this->hasOne(EmployeeDetails::class, 'user_id'); } public function projects(): HasMany { return $this->hasMany(Project::class, 'client_id'); } public function member(): HasMany { return $this->hasMany(ProjectMember::class, 'user_id'); } public function appreciations(): HasMany { return $this->hasMany(Appreciation::class, 'award_to'); } public function appreciationsGrouped(): HasMany { return $this->hasMany(Appreciation::class, 'award_to')->select('appreciations.*', DB::raw('count("award_id") as no_of_awards'))->groupBy('award_id'); } public function templateMember(): HasMany { return $this->hasMany(ProjectTemplateMember::class, 'user_id'); } public function role(): HasMany { return $this->hasMany(RoleUser::class, 'user_id'); } public function attendee(): HasMany { return $this->hasMany(EventAttendee::class, 'user_id'); } public function agent(): HasMany { return $this->hasMany(TicketAgentGroups::class, 'agent_id'); } public function agentGroup(): BelongsToMany { return $this->belongsToMany(TicketGroup::class, 'ticket_agent_groups', 'agent_id', 'group_id'); } public function agents(): HasMany { return $this->hasMany(Ticket::class, 'agent_id'); } public function leadAgent(): HasMany { return $this->hasMany(LeadAgent::class, 'user_id'); } public function group(): HasMany { return $this->hasMany(EmployeeTeam::class, 'user_id'); } public function country(): HasOne { return $this->hasOne(Country::class, 'id', 'country_id'); } public function skills(): array { return EmployeeSkill::select('skills.name')->join('skills', 'skills.id', 'employee_skills.skill_id')->where('user_id', $this->id)->pluck('name')->toArray(); } public function emergencyContacts(): HasMany { return $this->hasMany(EmergencyContact::class); } public function leaveTypes(): HasMany { return $this->hasMany(EmployeeLeaveQuota::class); } public function reportingTeam(): HasMany { return $this->hasMany(EmployeeDetails::class, 'reporting_to'); } public function tasks(): BelongsToMany { return $this->belongsToMany(Task::class, 'task_users'); } public function openTasks(): BelongsToMany { $taskBoardColumn = TaskboardColumn::completeColumn(); return $this->belongsToMany(Task::class, 'task_users')->where('tasks.board_column_id', '<>', $taskBoardColumn->id); } public function tickets(): HasMany { return $this->hasMany(Ticket::class, 'user_id')->orderBy('id', 'desc'); } public function supportTickets(): HasMany { return $this->hasMany(SupportTicket::class, 'user_id')->orderBy('id', 'desc'); } public function leaves(): HasMany { return $this->hasMany(Leave::class, 'user_id'); } public function documents(): HasMany { return $this->hasMany(EmployeeDocument::class, 'user_id'); } public function clientDocuments(): HasMany { return $this->hasMany(ClientDocument::class, 'user_id'); } public function visa(): HasMany { return $this->hasMany(VisaDetail::class, 'user_id'); } public function timeLogs(): HasMany { return $this->hasMany(ProjectTimeLog::class, 'user_id'); } // WORKSUITESAAS public function approvedCompany() { $company = $this->belongsTo(Company::class, 'company_id'); if (global_setting()->company_need_approval) { $company->where('companies.approved', 1); } return $company; } public static function allClients($exceptId = null, $active = false, $overRidePermission = null, $companyId = null) { if (!isRunningInConsoleOrSeeding() && !is_null($overRidePermission)) { $viewClientPermission = $overRidePermission; } elseif (!isRunningInConsoleOrSeeding() && user()) { $viewClientPermission = user()->permission('view_clients'); } if (isset($viewClientPermission) && $viewClientPermission == 'none') { return collect([]); } $clients = User::with('clientDetails') ->join('role_user', 'role_user.user_id', '=', 'users.id') ->join('roles', 'roles.id', '=', 'role_user.role_id') ->join('client_details', 'users.id', '=', 'client_details.user_id') ->select('users.id', 'users.name', 'users.created_at', 'client_details.company_name', 'users.image', 'users.email_notifications', 'users.mobile', 'users.country_id') ->where('roles.name', 'client'); if (!is_null($exceptId)) { if (is_array($exceptId)) { $clients->whereNotIn('users.id', $exceptId); } else { $clients->where('users.id', '<>', $exceptId); } } if (!$active) { $clients->withoutGlobalScope(ActiveScope::class); } if (!is_null($companyId)) { $clients->where('users.company_id', '<>', $companyId); } if (!isRunningInConsoleOrSeeding() && isset($viewClientPermission) && $viewClientPermission == 'added') { $clients->where('client_details.added_by', user()->id); } if (!isRunningInConsoleOrSeeding() && in_array('client', user_roles())) { $clients->where('client_details.user_id', user()->id); } return $clients->orderBy('users.name', 'asc')->get(); } public static function client() { return User::withoutGlobalScope(ActiveScope::class) ->with('clientDetails') ->join('role_user', 'role_user.user_id', '=', 'users.id') ->join('roles', 'roles.id', '=', 'role_user.role_id') ->join('client_details', 'users.id', '=', 'client_details.user_id') ->select('users.id', 'users.name', 'users.email', 'users.created_at', 'client_details.company_name', 'users.image', 'users.email_notifications', 'users.mobile', 'users.country_id') ->where('roles.name', 'client') ->where('users.id', user()->id) ->orderBy('users.name', 'asc') ->get(); } public static function allEmployees($exceptId = null, $active = false, $overRidePermission = null, $companyId = null) { if (!isRunningInConsoleOrSeeding() && !is_null($overRidePermission)) { $viewEmployeePermission = $overRidePermission; } elseif (!isRunningInConsoleOrSeeding() && user()) { $viewEmployeePermission = user()->permission('view_employees'); } $users = User::withRole('employee') ->join('employee_details', 'employee_details.user_id', '=', 'users.id') ->leftJoin('designations', 'employee_details.designation_id', '=', 'designations.id') ->select('users.id', 'users.company_id', 'users.name', 'users.created_at', 'users.image', 'designations.name as designation_name', 'users.email_notifications', 'users.mobile', 'users.country_id'); if (!is_null($exceptId)) { if (is_array($exceptId)) { $users->whereNotIn('users.id', $exceptId); } else { $users->where('users.id', '<>', $exceptId); } } if (!is_null($companyId)) { $users->where('users.company_id', $companyId); } if (!$active) { $users->withoutGlobalScope(ActiveScope::class); } if (!isRunningInConsoleOrSeeding() && user()) { if (isset($viewEmployeePermission)) { if (($viewEmployeePermission == 'added' && !in_array('client', user_roles()))) { $users->where(function ($q) { $q->where('employee_details.user_id', user()->id); $q->orWhere('employee_details.added_by', user()->id); }); } elseif ($viewEmployeePermission == 'owned' && !in_array('client', user_roles())) { $users->where('users.id', user()->id); } elseif ($viewEmployeePermission == 'both' && !in_array('client', user_roles())) { $users->where(function ($q) { $q->where('employee_details.user_id', user()->id); $q->orWhere('employee_details.added_by', user()->id); }); } elseif (($viewEmployeePermission == 'none' || $viewEmployeePermission == '') && !in_array('client', user_roles())) { $users->where('users.id', user()->id); } } if (in_array('client', user_roles())) { $clientEmployees = Project::where('client_id', user()->id) ->join('project_members', 'project_members.project_id', '=', 'projects.id') ->select('project_members.user_id') ->get() ->pluck('user_id'); $users->whereIn('users.id', $clientEmployees); } } if(!isRunningInConsoleOrSeeding() && user() && in_array('client', user_roles())) { $clientEmployess = Project::where('client_id', user()->id)->join('project_members', 'project_members.project_id', '=', 'projects.id') ->select('project_members.user_id')->get()->pluck('user_id'); $users->whereIn('users.id', $clientEmployess); } $users->orderBy('users.name'); $users->groupBy('users.id'); return $users->get(); } public static function allAdmins($companyId = null) { $users = User::withOut('clientDetails')->withRole('admin'); if (!is_null($companyId)) { return $users->where('users.company_id', $companyId)->get(); } return $users->get(); } public static function departmentUsers($teamId) { $users = User::join('employee_details', 'employee_details.user_id', '=', 'users.id') ->select('users.id', 'users.name', 'users.email', 'users.created_at') ->where('employee_details.department_id', $teamId); return $users->get(); } public static function userListLatest($userID, $term) { $termCnd = ''; if ($term) { $termCnd = 'and users.name like %' . $term . '%'; } $messageSetting = message_setting(); if (in_array('admin', user_roles())) { if ($messageSetting->allow_client_admin == 'no') { $termCnd .= "and roles.name != 'client'"; } } elseif (in_array('employee', user_roles())) { if ($messageSetting->allow_client_employee == 'no') { $termCnd .= "and roles.name != 'client'"; } } elseif (in_array('client', user_roles())) { if ($messageSetting->allow_client_admin == 'no') { $termCnd .= "and roles.name != 'admin'"; } if ($messageSetting->allow_client_employee == 'no') { $termCnd .= "and roles.name != 'employee'"; } } $query = DB::select('SELECT * FROM ( SELECT * FROM ( SELECT users.id,"0" AS groupId, users.name, users.image, users.email, users_chat.created_at as last_message, users_chat.message, users_chat.message_seen, users_chat.user_one FROM users INNER JOIN users_chat ON users_chat.from = users.id LEFT JOIN role_user ON role_user.user_id = users.id LEFT JOIN roles ON roles.id = role_user.role_id WHERE users_chat.to = ' . $userID . ' ' . $termCnd . ' UNION SELECT users.id,"0" AS groupId, users.name,users.image, users.email, users_chat.created_at as last_message, users_chat.message, users_chat.message_seen, users_chat.user_one FROM users INNER JOIN users_chat ON users_chat.to = users.id LEFT JOIN role_user ON role_user.user_id = users.id LEFT JOIN roles ON roles.id = role_user.role_id WHERE users_chat.from = ' . $userID . ' ' . $termCnd . ' ) AS allUsers ORDER BY last_message DESC ) AS allUsersSorted GROUP BY id ORDER BY last_message DESC'); return $query; } public static function isAdmin($userId) { $user = User::find($userId); if ($user) { return $user->hasRole('admin'); } return false; } public static function isClient($userId): bool { $user = User::find($userId); if ($user) { return $user->hasRole('client'); } return false; } public static function isEmployee($userId): bool { $user = User::find($userId); if ($user) { return $user->hasRole('employee'); } return false; } public static function firstSuperAdmin() { return User::withoutGlobalScopes(['active', CompanyScope::class]) ->where('is_superadmin', 1) ->whereNull('company_id') ->orderBy('id')->first(); } public static function allSuperAdmin() { return User::withoutGlobalScopes(['active', CompanyScope::class]) ->where('is_superadmin', 1) ->whereNull('company_id') ->get(); } public function getModulesAttribute() { return user_modules(); } public function sticky(): HasMany { return $this->hasMany(StickyNote::class, 'user_id')->orderBy('updated_at', 'desc'); } public function userChat(): HasMany { return $this->hasMany(UserChat::class, 'to')->where('message_seen', 'no'); } public function employeeDetails(): HasOne { return $this->hasOne(EmployeeDetails::class); } public function getUnreadNotificationsAttribute() { return $this->unreadNotifications()->get(); } /** * Check if user has a permission by its name. * * @param string|array $permission Permission string or array of permissions. * @param bool $requireAll All permissions in the array are required. * * @return bool */ public function can($permission, $requireAll = false) { config(['cache.default' => 'array']); if (is_array($permission)) { foreach ($permission as $permName) { $hasPerm = $this->can($permName); if ($hasPerm && !$requireAll) { return true; } if (!$hasPerm && $requireAll) { return false; } } // If we've made it this far and $requireAll is FALSE, then NONE of the perms were found // If we've made it this far and $requireAll is TRUE, then ALL of the perms were found. // Return the value of $requireAll; return $requireAll; } else { foreach ($this->cachedRoles() as $role) { // Validate against the Permission table foreach ($role->cachedPermissions() as $perm) { if (Str::is($permission, $perm->name)) { return true; } } } } config(['cache.default' => 'file']); return false; } public function getUserOtherRoleAttribute() { $userRole = null; $roles = cache()->remember( 'non-client-roles', 60 * 60 * 24, function () { return Role::where('name', '<>', 'client') ->orderBy('id', 'asc')->get(); } ); foreach ($roles as $role) { foreach ($this->role as $urole) { if ($role->id == $urole->role_id) { $userRole = $role->name; } if ($userRole == 'admin') { break; } } } return $userRole; } /** * @return false|mixed */ public function permission($permission) { return Cache::rememberForever('permission-' . $permission . '-' . $this->id, function () use ($permission) { $permissionType = UserPermission::join('permissions', 'user_permissions.permission_id', '=', 'permissions.id') ->join('permission_types', 'user_permissions.permission_type_id', '=', 'permission_types.id') ->select('permission_types.name') ->where('permissions.name', $permission) ->where('user_permissions.user_id', $this->id) ->first(); return $permissionType ? $permissionType->name : false; }); } public function permissionTypeId($permission) { return Cache::rememberForever('permission-id-' . $permission . '-' . $this->id, function () use ($permission) { $permissionType = UserPermission::join('permissions', 'user_permissions.permission_id', '=', 'permissions.id') ->join('permission_types', 'user_permissions.permission_type_id', '=', 'permission_types.id') ->select('permission_types.name', 'permission_types.id') ->where('permissions.name', $permission) ->where('user_permissions.user_id', $this->id) ->first(); return $permissionType ? $permissionType->name : false; }); } /** * @return BelongsToMany */ public function permissionTypes(): BelongsToMany { return $this->belongsToMany(Permission::class, 'user_permissions')->withTimestamps(); } /** * @return HasOne */ public function session(): HasOne { return $this->hasOne(Session::class, 'user_id')->select('user_id', 'ip_address', 'last_activity'); } /** * @return HasMany */ public function contracts(): HasMany { return $this->hasMany(Contract::class, 'client_id', 'id'); } public function assignUserRolePermission($roleId) { $rolePermissions = PermissionRole::where('role_id', $roleId)->get(); $data = []; UserPermission::where('user_id', $this->id)->delete(); foreach ($rolePermissions as $permission) { $data[] = [ 'permission_id' => $permission->permission_id, 'user_id' => $this->id, 'permission_type_id' => $permission->permission_type_id, ]; } foreach (array_chunk($data, 100) as $item) { UserPermission::insert($item); } } public function assignModuleRolePermission($module) { $module = Module::where('module_name', $module)->first(); if (!$module) { return true; } $rolePermissions = PermissionRole::join('permissions', 'permissions.id', '=', 'permission_role.permission_id') ->where('permissions.module_id', $module->id) ->select('permission_role.*') ->get(); foreach ($rolePermissions as $key => $value) { $userPermission = UserPermission::where('permission_id', $value->permission_id) ->where('user_id', $this->id) ->firstOrNew(); $userPermission->permission_id = $value->permission_id; $userPermission->user_id = $this->id; $userPermission->permission_type_id = $value->permission_type_id; $userPermission->save(); } } public function unreadMessages(): HasMany { return $this->hasMany(UserChat::class, 'from')->where('to', user()->id)->where('message_seen', 'no'); } public function shifts(): HasMany { return $this->hasMany(EmployeeShiftSchedule::class, 'user_id'); } public function employeeShift(): BelongsToMany { return $this->belongsToMany(EmployeeShift::class, 'employee_shift_schedules'); } public function userBadge() { $itsYou = '
' . __('app.itsYou') . '
'; /** @phpstan-ignore-next-line */ $salutation = ($this->salutation ? $this->salutation->label() . ' ' : ''); if (user() && user()->id == $this->id) { return $salutation . $this->name . $itsYou; } return $salutation . $this->name; } public function estimates(): HasMany { return $this->hasMany(Estimate::class, 'client_id'); } public function invoices(): HasMany { return $this->hasMany(Invoice::class, 'client_id'); } public function scopeOnlyEmployee($query) { return $query->whereHas('roles', function ($q) { $q->where('name', 'employee'); })->whereHas('employeeDetail'); } /** * Send the password reset notification. * * @param string $token * @return void */ public function sendPasswordResetNotification($token) { $this->notify(new ResetPassword($token)); } public static function allLeaveReportEmployees($exceptId = null, $active = false, $overRidePermission = null, $companyId = null) { if (!isRunningInConsoleOrSeeding() && !is_null($overRidePermission)) { $viewEmployeePermission = $overRidePermission; } elseif (!isRunningInConsoleOrSeeding() && user()) { $viewEmployeePermission = user()->permission('view_leave_report'); } $users = User::withRole('employee') ->join('employee_details', 'employee_details.user_id', '=', 'users.id') ->leftJoin('designations', 'employee_details.designation_id', '=', 'designations.id') ->select('users.id', 'users.company_id', 'users.name', 'users.email', 'users.created_at', 'users.image', 'designations.name as designation_name', 'users.email_notifications', 'users.mobile', 'users.country_id'); if (!is_null($exceptId)) { if (is_array($exceptId)) { $users->whereNotIn('users.id', $exceptId); } else { $users->where('users.id', '<>', $exceptId); } } if (!is_null($companyId)) { $users->where('users.company_id', $companyId); } if (!$active) { $users->withoutGlobalScope(ActiveScope::class); } if (!isRunningInConsoleOrSeeding() && user()) { if (isset($viewEmployeePermission)) { if ($viewEmployeePermission == 'added' && !in_array('client', user_roles())) { $users->where(function ($q) { $q->where('employee_details.added_by', user()->id); }); } elseif ($viewEmployeePermission == 'owned' && !in_array('client', user_roles())) { $users->where('users.id', user()->id); } elseif ($viewEmployeePermission == 'both' && !in_array('client', user_roles())) { $users->where(function ($q) { $q->where('employee_details.user_id', user()->id); $q->orWhere('employee_details.added_by', user()->id); }); } elseif (($viewEmployeePermission == 'none' || $viewEmployeePermission == '') && !in_array('client', user_roles())) { $users->where('users.id', user()->id); } } if (in_array('client', user_roles())) { $clientEmployees = Project::where('client_id', user()->id) ->join('project_members', 'project_members.project_id', '=', 'projects.id') ->select('project_members.user_id') ->get() ->pluck('user_id'); $users->whereIn('users.id', $clientEmployees); } } if(!isRunningInConsoleOrSeeding() && user() && in_array('client', user_roles())) { $clientEmployess = Project::where('client_id', user()->id)->join('project_members', 'project_members.project_id', '=', 'projects.id') ->select('project_members.user_id')->get()->pluck('user_id'); $users->whereIn('users.id', $clientEmployess); } $users->orderBy('users.name'); $users->groupBy('users.id'); return $users->get(); } }