blob: 84dd74b7f8b56065cbc171dcd3028f3ad207e838 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Models\ActiveDirectory;
4
5use Illuminate\Contracts\Auth\Authenticatable;
6use LdapRecord\Models\ActiveDirectory\Concerns\HasPrimaryGroup;
7use LdapRecord\Models\ActiveDirectory\Scopes\RejectComputerObjectClass;
8use LdapRecord\Models\Concerns\CanAuthenticate;
9use LdapRecord\Models\Concerns\HasPassword;
10use LdapRecord\Query\Model\Builder;
11
12class User extends Entry implements Authenticatable
13{
14 use HasPassword;
15 use HasPrimaryGroup;
16 use CanAuthenticate;
17
18 /**
19 * The password's attribute name.
20 *
21 * @var string
22 */
23 protected $passwordAttribute = 'unicodepwd';
24
25 /**
26 * The password's hash method.
27 *
28 * @var string
29 */
30 protected $passwordHashMethod = 'encode';
31
32 /**
33 * The object classes of the LDAP model.
34 *
35 * @var array
36 */
37 public static $objectClasses = [
38 'top',
39 'person',
40 'organizationalperson',
41 'user',
42 ];
43
44 /**
45 * The attributes that should be mutated to dates.
46 *
47 * @var array
48 */
49 protected $dates = [
50 'lastlogon' => 'windows-int',
51 'lastlogoff' => 'windows-int',
52 'pwdlastset' => 'windows-int',
53 'lockouttime' => 'windows-int',
54 'accountexpires' => 'windows-int',
55 'badpasswordtime' => 'windows-int',
56 'lastlogontimestamp' => 'windows-int',
57 ];
58
59 /**
60 * @inheritdoc
61 */
62 protected static function boot()
63 {
64 parent::boot();
65
66 // Here we will add a global scope to reject the 'computer' object
67 // class. This is needed due to computer objects containing all
68 // of the ActiveDirectory 'user' object classes. Without
69 // this scope, they would be included in results.
70 static::addGlobalScope(new RejectComputerObjectClass());
71 }
72
73 /**
74 * The groups relationship.
75 *
76 * Retrieves groups that the user is apart of.
77 *
78 * @return \LdapRecord\Models\Relations\HasMany
79 */
80 public function groups()
81 {
82 return $this->hasMany(Group::class, 'member')->with($this->primaryGroup());
83 }
84
85 /**
86 * The manager relationship.
87 *
88 * Retrieves the manager of the user.
89 *
90 * @return \LdapRecord\Models\Relations\HasOne
91 */
92 public function manager()
93 {
94 return $this->hasOne(static::class, 'manager');
95 }
96
97 /**
98 * The primary group relationship of the current user.
99 *
100 * Retrieves the primary group the user is apart of.
101 *
102 * @return \LdapRecord\Models\Relations\HasOne
103 */
104 public function primaryGroup()
105 {
106 return $this->hasOnePrimaryGroup(Group::class, 'primarygroupid');
107 }
108
109 /**
110 * Scopes the query to exchange mailbox users.
111 *
112 * @param Builder $query
113 *
114 * @return Builder
115 */
116 public function scopeWhereHasMailbox(Builder $query)
117 {
118 return $query->whereHas('msExchMailboxGuid');
119 }
120}