-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdriver.php
More file actions
121 lines (105 loc) · 3.04 KB
/
Copy pathdriver.php
File metadata and controls
121 lines (105 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php namespace Feather\Components\Auth;
use Hash;
use Cache;
use Feather\Core;
class Driver extends \Laravel\Auth\Drivers\Driver {
/**
* Fetch the current logged in user of Feather. If no user is logged in a default
* Guest user is returned.
*
* @param int $id
* @return object
*/
public function retrieve($id)
{
if(!is_null($id))
{
return Cache::remember("user_{$id}", function() use ($id)
{
$user = Core\User::with(array('roles'))->find($id);
// To provide a cleaner syntax when accessing roles, we'll make each of the roles keys
// the same as its ID.
$roles = array();
foreach($user->roles as $role) $roles[$role->id] = $role;
$user->relationships['roles'] = $roles;
return $user;
}, Core\User::cache_time);
}
else
{
return Cache::remember('guest', function()
{
return new Core\User(array(
'roles' => array(3 => Core\Role::find(3))
), true);
}, Core\User::cache_time);
}
}
/**
* Attempt to log a user in and if need be migrate them from an older system.
*
* @param array $credentials
* @return bool|object
*/
public function attempt($credentials = array())
{
// Find the user in the database with the username they have provided in the
// login form. If no user can be found then that's as far as we go.
if(!$user = Core\User::where_username($credentials['username'])->first())
{
return false;
}
// If there is an active migration from another forum we'll need to perform
// a couple of extra checks. A migrating user has only a single role, which
// is the migrating roll. If the user has not been migrated then we'll
// attempt to migrate them.
if($migration = Core\Migration::active())
{
$driver = Migrator\Drivers\Driver::make($migration);
if($user->roles()->only('roles.id') == 6 and ($old = $driver->login($credentials)))
{
// Because the users password is most likely hashed - it damn
// well better be - we'll replace the password key in the array
// with the password they logged in with. That way Feather can
// re-hash it when storing it.
$old = (object) array_merge((array) $old, array('password' => $credentials['password']));
// Finally attempt to migrate the user. If the migration fails it's
// logged so that the administrator can take further action if
// required.
if(!$user = $driver->migrate_user($old, $user))
{
dd('Failed to migrate user.');
return false;
}
}
else
{
return false;
}
}
else
{
// For a user that doesn't need to be migrated a simple hash check is in order.
if(!Hash::check($credentials['password'], $user->password))
{
return false;
}
}
return $this->login($user, array_get($credentials, 'remember'));
}
/**
* Log a user in to Feather.
*
* @param int|Feather\Core\User $user
* @param bool $remember
* @return bool
*/
public function login($user, $remember = false)
{
if($user instanceof Core\User)
{
$user = $user->id;
}
return parent::login($user, $remember);
}
}