1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\Core\Models;
9:
10: use Aurora\System\Classes\Model;
11: use Aurora\Modules\Core\Models\Tenant;
12:
13: /**
14: * The Core User class.
15: *
16: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
17: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
18: * @copyright Copyright (c) 2023, Afterlogic Corp.
19: * @property int $Id Object primary key
20: * @property string $UUID Unique identifier of the object
21: * @property string $Name User name
22: * @property string $PublicId User public ID, usually equals user's email address
23: * @property int $IdTenant ID of the tetant a user relates to
24: * @property bool $IsDisabled Reserved for future use
25: * @property int $IdSubscription @Deprecated since 9.7.0
26: * @property int $Role User role from \Aurora\System\Enums\UserRole
27: * @property \DateTime $LastLogin Date at time of last login
28: * @property string $LastLoginNow @Deprecated since 9.7.0
29: * @property int $LoginsCount Stores the number of times user has logged in
30: * @property string $Language Interface language set for this user
31: * @property int $TimeFormat Time format set for this user
32: * @property string $DateFormat Date format set for this user
33: * @property string $Question1 @Deprecated since 9.7.0
34: * @property string $Question2 @Deprecated since 9.7.0
35: * @property string $Answer1 @Deprecated since 9.7.0
36: * @property string $Answer2 @Deprecated since 9.7.0
37: * @property bool $SipEnable @Deprecated since 9.7.0
38: * @property string $SipImpi @Deprecated since 9.7.0
39: * @property string $SipPassword @Deprecated since 9.7.0
40: * @property bool $DesktopNotifications @Deprecated since 9.7.0
41: * @property string $Capa @Deprecated since 9.7.0
42: * @property string $CustomFields @Deprecated since 9.7.0
43: * @property bool $FilesEnable @Deprecated since 9.7.0
44: * @property string $EmailNotification @Deprecated since 9.7.0
45: * @property string $PasswordResetHash @Deprecated since 9.7.0
46: * @property bool $WriteSeparateLog If set to true, a separate log file is recorded for the user
47: * @property string $DefaultTimeZone Default time zone set for this user
48: * @property int $TokensValidFromTimestamp Timestamp the token is valid since
49: * @property array $Properties Custom properties for use by other modules
50: * @property string $Note User text note
51: * @property \Illuminate\Support\Carbon|null $CreatedAt
52: * @property \Illuminate\Support\Carbon|null $UpdatedAt
53: * @property-read \Illuminate\Database\Eloquent\Collection<int, \Aurora\Modules\Core\Models\Group> $Groups
54: * @property-read int|null $groups_count
55: * @property-read mixed $entity_id
56: * @property-read Tenant|null $parent
57: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Core\Models\User firstWhere(Closure|string|array|\Illuminate\Database\Query\Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
58: * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
59: * @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
60: * @method static \Illuminate\Database\Eloquent\Builder|User query()
61: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Core\Models\User where(Closure|string|array|\Illuminate\Database\Query\Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and')
62: * @method static \Illuminate\Database\Eloquent\Builder|User whereAnswer1($value)
63: * @method static \Illuminate\Database\Eloquent\Builder|User whereAnswer2($value)
64: * @method static \Illuminate\Database\Eloquent\Builder|User whereCapa($value)
65: * @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
66: * @method static \Illuminate\Database\Eloquent\Builder|User whereCustomFields($value)
67: * @method static \Illuminate\Database\Eloquent\Builder|User whereDateFormat($value)
68: * @method static \Illuminate\Database\Eloquent\Builder|User whereDefaultTimeZone($value)
69: * @method static \Illuminate\Database\Eloquent\Builder|User whereDesktopNotifications($value)
70: * @method static \Illuminate\Database\Eloquent\Builder|User whereEmailNotification($value)
71: * @method static \Illuminate\Database\Eloquent\Builder|User whereFilesEnable($value)
72: * @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
73: * @method static \Illuminate\Database\Eloquent\Builder|User whereIdSubscription($value)
74: * @method static \Illuminate\Database\Eloquent\Builder|User whereIdTenant($value)
75: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Core\Models\User whereIn(string $column, mixed $values, string $boolean = 'and', bool $not = false)
76: * @method static \Illuminate\Database\Eloquent\Builder|User whereIsDisabled($value)
77: * @method static \Illuminate\Database\Eloquent\Builder|User whereLanguage($value)
78: * @method static \Illuminate\Database\Eloquent\Builder|User whereLastLogin($value)
79: * @method static \Illuminate\Database\Eloquent\Builder|User whereLastLoginNow($value)
80: * @method static \Illuminate\Database\Eloquent\Builder|User whereLoginsCount($value)
81: * @method static \Illuminate\Database\Eloquent\Builder|User whereName($value)
82: * @method static \Illuminate\Database\Eloquent\Builder|User wherePasswordResetHash($value)
83: * @method static \Illuminate\Database\Eloquent\Builder|User whereProperties($value)
84: * @method static \Illuminate\Database\Eloquent\Builder|User wherePublicId($value)
85: * @method static \Illuminate\Database\Eloquent\Builder|User whereQuestion1($value)
86: * @method static \Illuminate\Database\Eloquent\Builder|User whereQuestion2($value)
87: * @method static \Illuminate\Database\Eloquent\Builder|User whereRole($value)
88: * @method static \Illuminate\Database\Eloquent\Builder|User whereSipEnable($value)
89: * @method static \Illuminate\Database\Eloquent\Builder|User whereSipImpi($value)
90: * @method static \Illuminate\Database\Eloquent\Builder|User whereSipPassword($value)
91: * @method static \Illuminate\Database\Eloquent\Builder|User whereTimeFormat($value)
92: * @method static \Illuminate\Database\Eloquent\Builder|User whereTokensValidFromTimestamp($value)
93: * @method static \Illuminate\Database\Eloquent\Builder|User whereUUID($value)
94: * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
95: * @method static \Illuminate\Database\Eloquent\Builder|User whereWriteSeparateLog($value)
96: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Core\Models\User find(int|string $id, array|string $columns = ['*'])
97: * @method static int count(string $columns = '*')
98: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Core\Models\User findOrFail(int|string $id, mixed $id, Closure|array|string $columns = ['*'], Closure $callback = null)
99: * @method static \Illuminate\Database\Eloquent\Builder|\Aurora\Modules\Core\Models\User first(array|string $columns = ['*'])
100: * @mixin \Eloquent
101: */
102: class User extends Model
103: {
104: protected $table = 'core_users';
105:
106: protected $moduleName = 'Core';
107:
108: protected $parentType = Tenant::class;
109:
110: protected $parentKey = 'IdTenant';
111:
112: protected $parentInheritedAttributes = [
113: ];
114:
115: protected $foreignModel = Tenant::class;
116: protected $foreignModelIdColumn = 'IdTenant'; // Column that refers to an external table
117:
118: /**
119: * The attributes that are mass assignable.
120: *
121: * @var array
122: */
123: protected $fillable = [
124: 'Id',
125: 'UUID',
126: 'Name',
127: 'PublicId',
128: 'IdTenant',
129: 'IsDisabled',
130: 'IdSubscription',
131: 'Role',
132: 'LastLogin',
133: 'LastLoginNow',
134: 'LoginsCount',
135: 'Language',
136: 'TimeFormat',
137: 'DateFormat',
138: 'Question1',
139: 'Question2',
140: 'Answer1',
141: 'Answer2',
142: 'SipEnable',
143: 'SipImpi',
144: 'SipPassword',
145: 'DesktopNotifications',
146: 'Capa',
147: 'CustomFields',
148: 'FilesEnable',
149: 'EmailNotification',
150: 'PasswordResetHash',
151: 'WriteSeparateLog',
152: 'DefaultTimeZone',
153: 'TokensValidFromTimestamp',
154: 'Properties',
155: 'Note',
156: ];
157:
158: /**
159: * @var array
160: * */
161: protected $validationRules = [
162: 'TimeFormat' => 'in:0,1',
163: ];
164:
165: /**
166: * @var array
167: */
168: protected $validationMessages = [
169: 'TimeFormat.in' => 'User model validation error, rule: TimeFormat'
170: ];
171:
172: /**
173: * The attributes that should be hidden for arrays.
174: *
175: * @var array
176: */
177: protected $hidden = [
178: ];
179:
180: protected $casts = [
181: 'Properties' => 'array',
182: 'IsDisabled' => 'boolean',
183: 'SipEnable' => 'boolean',
184: 'DesktopNotifications' => 'boolean',
185: 'FilesEnable' => 'boolean',
186: 'WriteSeparateLog' => 'boolean',
187: 'LastLogin' => 'datetime'
188: ];
189:
190: protected $attributes = [
191: ];
192:
193: /**
194: * Checks if a user is a superadmin.
195: *
196: * return bool
197: */
198: public function isAdmin()
199: {
200: return $this->Id === -1;
201: }
202:
203: /**
204: * Checks if a user can act as a regular user.
205: *
206: * return bool
207: */
208: public function isNormalOrTenant()
209: {
210: return $this->Role === \Aurora\System\Enums\UserRole::NormalUser || $this->Role === \Aurora\System\Enums\UserRole::TenantAdmin;
211: }
212:
213: /**
214: * Returns array of groups the user is related to.
215: *
216: * return array
217: */
218: public function Groups()
219: {
220: return $this->belongsToMany(Group::class, 'core_group_user', 'UserId', 'GroupId');
221: }
222: }
223: