]> BookStack Code Mirror - bookstack/blob - app/Providers/LdapUserProvider.php
New strings
[bookstack] / app / Providers / LdapUserProvider.php
1 <?php
2
3 namespace BookStack\Providers;
4
5 use BookStack\Role;
6 use BookStack\Services\LdapService;
7 use BookStack\User;
8 use Illuminate\Contracts\Auth\Authenticatable;
9 use Illuminate\Contracts\Auth\UserProvider;
10
11 class LdapUserProvider implements UserProvider
12 {
13
14     /**
15      * The user model.
16      *
17      * @var string
18      */
19     protected $model;
20
21     /**
22      * @var LdapService
23      */
24     protected $ldapService;
25
26
27     /**
28      * LdapUserProvider constructor.
29      * @param             $model
30      * @param LdapService $ldapService
31      */
32     public function __construct($model, LdapService $ldapService)
33     {
34         $this->model = $model;
35         $this->ldapService = $ldapService;
36     }
37
38     /**
39      * Create a new instance of the model.
40      *
41      * @return \Illuminate\Database\Eloquent\Model
42      */
43     public function createModel()
44     {
45         $class = '\\' . ltrim($this->model, '\\');
46         return new $class;
47     }
48
49
50     /**
51      * Retrieve a user by their unique identifier.
52      *
53      * @param  mixed $identifier
54      * @return \Illuminate\Contracts\Auth\Authenticatable|null
55      */
56     public function retrieveById($identifier)
57     {
58         return $this->createModel()->newQuery()->find($identifier);
59     }
60
61     /**
62      * Retrieve a user by their unique identifier and "remember me" token.
63      *
64      * @param  mixed  $identifier
65      * @param  string $token
66      * @return \Illuminate\Contracts\Auth\Authenticatable|null
67      */
68     public function retrieveByToken($identifier, $token)
69     {
70         $model = $this->createModel();
71
72         return $model->newQuery()
73             ->where($model->getAuthIdentifierName(), $identifier)
74             ->where($model->getRememberTokenName(), $token)
75             ->first();
76     }
77
78
79     /**
80      * Update the "remember me" token for the given user in storage.
81      *
82      * @param  \Illuminate\Contracts\Auth\Authenticatable $user
83      * @param  string                                     $token
84      * @return void
85      */
86     public function updateRememberToken(Authenticatable $user, $token)
87     {
88         if ($user->exists) {
89             $user->setRememberToken($token);
90             $user->save();
91         }
92     }
93
94     /**
95      * Retrieve a user by the given credentials.
96      *
97      * @param  array $credentials
98      * @return \Illuminate\Contracts\Auth\Authenticatable|null
99      */
100     public function retrieveByCredentials(array $credentials)
101     {
102         // Get user via LDAP
103         $userDetails = $this->ldapService->getUserDetails($credentials['username']);
104         if ($userDetails === null) {
105             return null;
106         }
107
108         // Search current user base by looking up a uid
109         $model = $this->createModel();
110         $currentUser = $model->newQuery()
111             ->where('external_auth_id', $userDetails['uid'])
112             ->first();
113
114         if ($currentUser !== null) {
115             return $currentUser;
116         }
117
118         $model->name = $userDetails['name'];
119         $model->external_auth_id = $userDetails['uid'];
120         $model->email = $userDetails['email'];
121         $model->email_confirmed = false;
122         return $model;
123     }
124
125     /**
126      * Validate a user against the given credentials.
127      *
128      * @param  \Illuminate\Contracts\Auth\Authenticatable $user
129      * @param  array                                      $credentials
130      * @return bool
131      */
132     public function validateCredentials(Authenticatable $user, array $credentials)
133     {
134         return $this->ldapService->validateUserCredentials($user, $credentials['username'], $credentials['password']);
135     }
136 }