belongsTo(Account::class); } /** * Returns all company tokens. * * @return Collection */ public function tokens() { return $this->hasMany(CompanyToken::class)->orderBy('id', 'ASC'); } /** * Return first user token * * @return token object */ public function token() { return $this->tokens()->first(); } /** * 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'); } /** * Returns the current company * * @return Collection */ public function company() { return $this->tokens()->whereRaw("BINARY `token`= ?", [request()->header('X-API-TOKEN')])->first()->company; } /** * Returns the pivot tables for Company / User * * @return Collection * */ public function user_companies() { return $this->hasMany(CompanyUser::class); } /** * Returns the current company by * querying directly on the pivot table relationship * * @return Collection * @deprecated */ public function user_company() { return $this->user_companies->where('company_id', $this->companyId())->first(); } /** * 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->user_company()->permissions); if (! $permissions) return []; return $permissions; } /** * Returns a object of User Settings * * @return stdClass */ public function settings() { return json_decode($this->user_company()->settings); } /** * Returns a boolean of the administrator status of the user * * @return bool */ public function isAdmin() : bool { return $this->user_company()->is_admin; } /** * Returns all user created contacts * * @return Collection */ public function contacts() { return $this->hasMany(Contact::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; } /** * 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 $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'); } }