'object', 'permissions' => 'object', 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', //'last_login' => 'timestamp', ]; public function getHashedIdAttribute() { return $this->encodePrimaryKey($this->id); } /** * Returns a account. * * @return Collection */ public function account() { return $this->belongsTo(Account::class); } /** * Returns all company tokens. * * @return Collection */ public function tokens() { return $this->hasMany(CompanyToken::class)->orderBy('id', 'ASC'); } /** * Returns all companies a user has access to. * * @return Collection */ public function companies() { return $this->belongsToMany(Company::class)->using(CompanyUser::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked'); } /** * * As we are authenticating on CompanyToken, * we need to link the company to the user manually. This allows * us to decouple a $user and their attached companies. * */ public function setCompany($company) { $this->company = $company; } /** * Returns the currently set Company */ public function getCompany() { return $this->company; } /** * Returns the current company * * @return Collection */ public function company() { return $this->getCompany(); } private function setCompanyByGuard() { if(Auth::guard('contact')->check()) $this->setCompany(auth()->user()->client->company); } public function company_users() { return $this->hasMany(CompanyUser::class); } public function company_user() { return $this->hasOneThrough(CompanyUser::class, CompanyToken::class, 'user_id', 'company_id','id','company_id')->where('company_user.user_id', $this->id); } /** * Returns the currently set company id for the user * * @return int */ public function companyId() :int { return $this->company()->id; } /** * Returns a object of user permissions * * @return stdClass */ public function permissions() { $permissions = json_decode($this->company_user->permissions); if (! $permissions) return []; return $permissions; } /** * Returns a object of User Settings * * @return stdClass */ public function settings() { return json_decode($this->company_user->settings); } /** * Returns a boolean of the administrator status of the user * * @return bool */ public function isAdmin() : bool { return $this->company_user->is_admin; } /** * Returns all user created contacts * * @return Collection */ public function contacts() { return $this->hasMany(ClientContact::class); } /** * Returns a boolean value if the user owns the current Entity * * @param string Entity * @return bool */ public function owns($entity) : bool { return ! empty($entity->user_id) && $entity->user_id == $this->id; } /** * Returns a boolean value if the user is assigned to the current Entity * * @param string Entity * @return bool */ public function assigned($entity) : bool { return ! empty($entity->assigned_user_id) && $entity->assigned_user_id == $this->id; } /** * Flattens a stdClass representation of the User Permissions * into a Collection * * @return Collection */ public function permissionsFlat() :Collection { return collect($this->permissions())->flatten(); } /** * Returns true if permissions exist in the map * * @param string permission * @return boolean */ public function hasPermission($permission) : bool { return (stripos($this->company_user->permissions, $permission) !== false); // return $this->permissionsFlat()->contains($permission); } /** * Returns a array of permission for the mobile application * * @return array */ public function permissionsMap() : array { $keys = array_values((array) $this->permissions()); $values = array_fill(0, count($keys), true); return array_combine($keys, $values); } public function documents() { return $this->morphMany(Document::class, 'documentable'); } public function getEmailVerifiedAt() { if($this->email_verified_at) return Carbon::parse($this->email_verified_at)->timestamp; else return null; } public function routeNotificationForSlack($notification) { //todo need to return the company channel here for hosted users //else the env variable for selfhosted return config('ninja.notification.slack'); } public function routeNotificationForMail($notification) { return $this->email; } /** * Retrieve the model for a bound value. * * @param mixed $value * @return \Illuminate\Database\Eloquent\Model|null */ public function resolveRouteBinding($value) { return $this ->withTrashed() ->where('id', $this->decodePrimaryKey($value))->firstOrFail(); } }