@@ -23,50 +23,50 @@ |
||
23 | 23 | * @return User |
24 | 24 | * @throws \RuntimeException Usually only if overriding data fails to validate |
25 | 25 | */ |
26 | - protected function createAuthUser(bool $login = true, array $overrides = []): User |
|
27 | - { |
|
28 | - $fabricator = new Fabricator(UserFaker::class); |
|
26 | + protected function createAuthUser(bool $login = true, array $overrides = []): User |
|
27 | + { |
|
28 | + $fabricator = new Fabricator(UserFaker::class); |
|
29 | 29 | |
30 | - // Set overriding data, if necessary |
|
31 | - if (! empty($overrides)) |
|
32 | - { |
|
33 | - $fabricator->setOverrides($overrides); |
|
34 | - } |
|
30 | + // Set overriding data, if necessary |
|
31 | + if (! empty($overrides)) |
|
32 | + { |
|
33 | + $fabricator->setOverrides($overrides); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @var User $user |
|
38 | - */ |
|
39 | - $user = $fabricator->make(); |
|
40 | - $user->activate(); |
|
36 | + /** |
|
37 | + * @var User $user |
|
38 | + */ |
|
39 | + $user = $fabricator->make(); |
|
40 | + $user->activate(); |
|
41 | 41 | |
42 | - if (! $userId = model(UserFaker::class)->insert($user)) |
|
43 | - { |
|
44 | - $error = implode(' ', model(UserFaker::class)->errors()); |
|
42 | + if (! $userId = model(UserFaker::class)->insert($user)) |
|
43 | + { |
|
44 | + $error = implode(' ', model(UserFaker::class)->errors()); |
|
45 | 45 | |
46 | - throw new \RuntimeException('Unable to create user: ' . $error); |
|
47 | - } |
|
46 | + throw new \RuntimeException('Unable to create user: ' . $error); |
|
47 | + } |
|
48 | 48 | |
49 | - // Look the user up using Model Factory in case it is overridden in App |
|
50 | - $user = model(UserModel::class)->find($userId); |
|
49 | + // Look the user up using Model Factory in case it is overridden in App |
|
50 | + $user = model(UserModel::class)->find($userId); |
|
51 | 51 | |
52 | - if ($login) |
|
53 | - { |
|
54 | - $auth = Services::authentication(); |
|
55 | - $auth->login($user); |
|
56 | - Services::injectMock('authentication', $auth); |
|
57 | - } |
|
52 | + if ($login) |
|
53 | + { |
|
54 | + $auth = Services::authentication(); |
|
55 | + $auth->login($user); |
|
56 | + Services::injectMock('authentication', $auth); |
|
57 | + } |
|
58 | 58 | |
59 | - return $user; |
|
60 | - } |
|
59 | + return $user; |
|
60 | + } |
|
61 | 61 | |
62 | 62 | /** |
63 | 63 | * Resets the Authentication and Authorization services. |
64 | 64 | * Particularly helpful between feature tests. |
65 | 65 | */ |
66 | - protected function resetAuthServices() |
|
67 | - { |
|
68 | - Services::injectMock('authentication', Services::authentication('local', null, null, false)); |
|
69 | - Services::injectMock('authorization', Services::authorization(null, null, null, false)); |
|
70 | - $_SESSION = []; |
|
71 | - } |
|
66 | + protected function resetAuthServices() |
|
67 | + { |
|
68 | + Services::injectMock('authentication', Services::authentication('local', null, null, false)); |
|
69 | + Services::injectMock('authorization', Services::authorization(null, null, null, false)); |
|
70 | + $_SESSION = []; |
|
71 | + } |
|
72 | 72 | } |
@@ -5,18 +5,18 @@ |
||
5 | 5 | |
6 | 6 | class PermissionFaker extends PermissionModel |
7 | 7 | { |
8 | - /** |
|
9 | - * Faked data for Fabricator. |
|
10 | - * |
|
11 | - * @param Generator $faker |
|
12 | - * |
|
13 | - * @return array |
|
14 | - */ |
|
15 | - public function fake(Generator &$faker): array |
|
16 | - { |
|
17 | - return [ |
|
8 | + /** |
|
9 | + * Faked data for Fabricator. |
|
10 | + * |
|
11 | + * @param Generator $faker |
|
12 | + * |
|
13 | + * @return array |
|
14 | + */ |
|
15 | + public function fake(Generator &$faker): array |
|
16 | + { |
|
17 | + return [ |
|
18 | 18 | 'name' => $faker->word, |
19 | 19 | 'description' => $faker->sentence, |
20 | - ]; |
|
21 | - } |
|
20 | + ]; |
|
21 | + } |
|
22 | 22 | } |
@@ -6,19 +6,19 @@ |
||
6 | 6 | |
7 | 7 | class UserFaker extends UserModel |
8 | 8 | { |
9 | - /** |
|
10 | - * Faked data for Fabricator. |
|
11 | - * |
|
12 | - * @param Generator $faker |
|
13 | - * |
|
14 | - * @return User |
|
15 | - */ |
|
16 | - public function fake(Generator &$faker): User |
|
17 | - { |
|
18 | - return new User([ |
|
19 | - 'email' => $faker->email, |
|
20 | - 'username' => $faker->userName, |
|
21 | - 'password' => bin2hex(random_bytes(16)), |
|
22 | - ]); |
|
23 | - } |
|
9 | + /** |
|
10 | + * Faked data for Fabricator. |
|
11 | + * |
|
12 | + * @param Generator $faker |
|
13 | + * |
|
14 | + * @return User |
|
15 | + */ |
|
16 | + public function fake(Generator &$faker): User |
|
17 | + { |
|
18 | + return new User([ |
|
19 | + 'email' => $faker->email, |
|
20 | + 'username' => $faker->userName, |
|
21 | + 'password' => bin2hex(random_bytes(16)), |
|
22 | + ]); |
|
23 | + } |
|
24 | 24 | } |
@@ -6,18 +6,18 @@ |
||
6 | 6 | |
7 | 7 | class GroupFaker extends GroupModel |
8 | 8 | { |
9 | - /** |
|
10 | - * Faked data for Fabricator. |
|
11 | - * |
|
12 | - * @param Generator $faker |
|
13 | - * |
|
14 | - * @return stdClass |
|
15 | - */ |
|
16 | - public function fake(Generator &$faker): stdClass |
|
17 | - { |
|
18 | - return (object) [ |
|
9 | + /** |
|
10 | + * Faked data for Fabricator. |
|
11 | + * |
|
12 | + * @param Generator $faker |
|
13 | + * |
|
14 | + * @return stdClass |
|
15 | + */ |
|
16 | + public function fake(Generator &$faker): stdClass |
|
17 | + { |
|
18 | + return (object) [ |
|
19 | 19 | 'name' => $faker->word, |
20 | 20 | 'description' => $faker->sentence, |
21 | - ]; |
|
22 | - } |
|
21 | + ]; |
|
22 | + } |
|
23 | 23 | } |
@@ -4,67 +4,67 @@ discard block |
||
4 | 4 | |
5 | 5 | if (! function_exists('logged_in')) |
6 | 6 | { |
7 | - /** |
|
8 | - * Checks to see if the user is logged in. |
|
9 | - * |
|
10 | - * @return bool |
|
11 | - */ |
|
12 | - function logged_in() |
|
13 | - { |
|
14 | - return service('authentication')->check(); |
|
15 | - } |
|
7 | + /** |
|
8 | + * Checks to see if the user is logged in. |
|
9 | + * |
|
10 | + * @return bool |
|
11 | + */ |
|
12 | + function logged_in() |
|
13 | + { |
|
14 | + return service('authentication')->check(); |
|
15 | + } |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | if (! function_exists('user')) |
19 | 19 | { |
20 | - /** |
|
21 | - * Returns the User instance for the current logged in user. |
|
22 | - * |
|
23 | - * @return User|null |
|
24 | - */ |
|
25 | - function user() |
|
26 | - { |
|
27 | - $authenticate = service('authentication'); |
|
28 | - $authenticate->check(); |
|
29 | - return $authenticate->user(); |
|
30 | - } |
|
20 | + /** |
|
21 | + * Returns the User instance for the current logged in user. |
|
22 | + * |
|
23 | + * @return User|null |
|
24 | + */ |
|
25 | + function user() |
|
26 | + { |
|
27 | + $authenticate = service('authentication'); |
|
28 | + $authenticate->check(); |
|
29 | + return $authenticate->user(); |
|
30 | + } |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | if (! function_exists('user_id')) |
34 | 34 | { |
35 | - /** |
|
36 | - * Returns the User ID for the current logged in user. |
|
37 | - * |
|
38 | - * @return int|null |
|
39 | - */ |
|
40 | - function user_id() |
|
41 | - { |
|
42 | - $authenticate = service('authentication'); |
|
43 | - $authenticate->check(); |
|
44 | - return $authenticate->id(); |
|
45 | - } |
|
35 | + /** |
|
36 | + * Returns the User ID for the current logged in user. |
|
37 | + * |
|
38 | + * @return int|null |
|
39 | + */ |
|
40 | + function user_id() |
|
41 | + { |
|
42 | + $authenticate = service('authentication'); |
|
43 | + $authenticate->check(); |
|
44 | + return $authenticate->id(); |
|
45 | + } |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | if (! function_exists('in_groups')) |
49 | 49 | { |
50 | - /** |
|
51 | - * Ensures that the current user is in at least one of the passed in |
|
52 | - * groups. The groups can be passed in as either ID's or group names. |
|
53 | - * You can pass either a single item or an array of items. |
|
54 | - * |
|
55 | - * Example: |
|
56 | - * in_groups([1, 2, 3]); |
|
57 | - * in_groups(14); |
|
58 | - * in_groups('admins'); |
|
59 | - * in_groups( ['admins', 'moderators'] ); |
|
60 | - * |
|
61 | - * @param mixed $groups |
|
62 | - * |
|
63 | - * @return bool |
|
64 | - */ |
|
65 | - function in_groups($groups): bool |
|
66 | - { |
|
67 | - $authenticate = service('authentication'); |
|
50 | + /** |
|
51 | + * Ensures that the current user is in at least one of the passed in |
|
52 | + * groups. The groups can be passed in as either ID's or group names. |
|
53 | + * You can pass either a single item or an array of items. |
|
54 | + * |
|
55 | + * Example: |
|
56 | + * in_groups([1, 2, 3]); |
|
57 | + * in_groups(14); |
|
58 | + * in_groups('admins'); |
|
59 | + * in_groups( ['admins', 'moderators'] ); |
|
60 | + * |
|
61 | + * @param mixed $groups |
|
62 | + * |
|
63 | + * @return bool |
|
64 | + */ |
|
65 | + function in_groups($groups): bool |
|
66 | + { |
|
67 | + $authenticate = service('authentication'); |
|
68 | 68 | $authorize = service('authorization'); |
69 | 69 | |
70 | 70 | if ($authenticate->check()) |
@@ -73,22 +73,22 @@ discard block |
||
73 | 73 | } |
74 | 74 | |
75 | 75 | return false; |
76 | - } |
|
76 | + } |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | if (! function_exists('has_permission')) |
80 | 80 | { |
81 | - /** |
|
82 | - * Ensures that the current user has the passed in permission. |
|
83 | - * The permission can be passed in either as an ID or name. |
|
84 | - * |
|
85 | - * @param int|string $permission |
|
86 | - * |
|
87 | - * @return bool |
|
88 | - */ |
|
89 | - function has_permission($permission): bool |
|
90 | - { |
|
91 | - $authenticate = service('authentication'); |
|
81 | + /** |
|
82 | + * Ensures that the current user has the passed in permission. |
|
83 | + * The permission can be passed in either as an ID or name. |
|
84 | + * |
|
85 | + * @param int|string $permission |
|
86 | + * |
|
87 | + * @return bool |
|
88 | + */ |
|
89 | + function has_permission($permission): bool |
|
90 | + { |
|
91 | + $authenticate = service('authentication'); |
|
92 | 92 | $authorize = service('authorization'); |
93 | 93 | |
94 | 94 | if ($authenticate->check()) |
@@ -97,5 +97,5 @@ discard block |
||
97 | 97 | } |
98 | 98 | |
99 | 99 | return false; |
100 | - } |
|
100 | + } |
|
101 | 101 | } |
@@ -6,53 +6,53 @@ |
||
6 | 6 | |
7 | 7 | class SetPassword extends BaseCommand |
8 | 8 | { |
9 | - protected $group = 'Auth'; |
|
10 | - protected $name = 'auth:set_password'; |
|
11 | - protected $description = 'Set password to user.'; |
|
12 | - |
|
13 | - protected $usage = 'auth:set_password [identity] [password]'; |
|
14 | - protected $arguments = [ |
|
15 | - 'identity' => 'User identity.', |
|
16 | - 'password' => 'Password value you want to set.', |
|
17 | - ]; |
|
18 | - |
|
19 | - public function run(array $params = []) |
|
20 | - { |
|
21 | - // Consume or prompt for password |
|
22 | - $identity = isset($params[0]) ? $params[0] : null; |
|
23 | - $password = isset($params[1]) ? $params[1] : null; |
|
24 | - |
|
25 | - if (empty($identity)) |
|
26 | - { |
|
27 | - $identity = CLI::prompt('Identity', null, 'required'); |
|
28 | - } |
|
29 | - |
|
30 | - if (empty($password)) |
|
31 | - { |
|
32 | - $password = CLI::prompt('Password', null, 'required'); |
|
33 | - } |
|
34 | - |
|
35 | - $type = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; |
|
36 | - |
|
37 | - $userModel = new UserModel(); |
|
38 | - $user = $userModel->where($type, $identity)->first(); |
|
39 | - |
|
40 | - if (! $user) |
|
41 | - { |
|
9 | + protected $group = 'Auth'; |
|
10 | + protected $name = 'auth:set_password'; |
|
11 | + protected $description = 'Set password to user.'; |
|
12 | + |
|
13 | + protected $usage = 'auth:set_password [identity] [password]'; |
|
14 | + protected $arguments = [ |
|
15 | + 'identity' => 'User identity.', |
|
16 | + 'password' => 'Password value you want to set.', |
|
17 | + ]; |
|
18 | + |
|
19 | + public function run(array $params = []) |
|
20 | + { |
|
21 | + // Consume or prompt for password |
|
22 | + $identity = isset($params[0]) ? $params[0] : null; |
|
23 | + $password = isset($params[1]) ? $params[1] : null; |
|
24 | + |
|
25 | + if (empty($identity)) |
|
26 | + { |
|
27 | + $identity = CLI::prompt('Identity', null, 'required'); |
|
28 | + } |
|
29 | + |
|
30 | + if (empty($password)) |
|
31 | + { |
|
32 | + $password = CLI::prompt('Password', null, 'required'); |
|
33 | + } |
|
34 | + |
|
35 | + $type = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; |
|
36 | + |
|
37 | + $userModel = new UserModel(); |
|
38 | + $user = $userModel->where($type, $identity)->first(); |
|
39 | + |
|
40 | + if (! $user) |
|
41 | + { |
|
42 | 42 | CLI::write('User with identity: '. $identity .' not found.', 'red'); |
43 | - } |
|
44 | - else |
|
45 | - { |
|
46 | - $user->password = $password; |
|
43 | + } |
|
44 | + else |
|
45 | + { |
|
46 | + $user->password = $password; |
|
47 | 47 | |
48 | - if ($userModel->save($user)) |
|
49 | - { |
|
48 | + if ($userModel->save($user)) |
|
49 | + { |
|
50 | 50 | CLI::write('Password successfully set for user with identity: ' . $identity , 'green'); |
51 | - } |
|
52 | - else |
|
53 | - { |
|
51 | + } |
|
52 | + else |
|
53 | + { |
|
54 | 54 | CLI::write('Failed to set password for user with identity: ' . $identity , 'red'); |
55 | - } |
|
56 | - } |
|
57 | - } |
|
55 | + } |
|
56 | + } |
|
57 | + } |
|
58 | 58 | } |
@@ -190,7 +190,7 @@ |
||
190 | 190 | protected function publishView($view, string $prefix = '') |
191 | 191 | { |
192 | 192 | $path = "{$this->sourcePath}/Views/{$prefix}{$view}"; |
193 | - $namespace = defined('APP_NAMESPACE') ? APP_NAMESPACE : 'App'; |
|
193 | + $namespace = defined('APP_NAMESPACE') ? APP_NAMESPACE : 'App'; |
|
194 | 194 | |
195 | 195 | $content = file_get_contents($path); |
196 | 196 | $content = str_replace('Myth\Auth\Views', $namespace.'\Auth', $content); |
@@ -12,22 +12,22 @@ |
||
12 | 12 | |
13 | 13 | public function run(array $params) |
14 | 14 | { |
15 | - $db = db_connect(); |
|
15 | + $db = db_connect(); |
|
16 | 16 | |
17 | - // get all groups |
|
18 | - $rows = $db->table('auth_groups') |
|
19 | - ->select('id, name, description') |
|
20 | - ->orderBy('name', 'asc') |
|
21 | - ->get()->getResultArray(); |
|
17 | + // get all groups |
|
18 | + $rows = $db->table('auth_groups') |
|
19 | + ->select('id, name, description') |
|
20 | + ->orderBy('name', 'asc') |
|
21 | + ->get()->getResultArray(); |
|
22 | 22 | |
23 | - if (empty($rows)) |
|
24 | - { |
|
25 | - CLI::write( CLI::color("There are no groups.", 'yellow') ); |
|
26 | - } |
|
27 | - else |
|
28 | - { |
|
29 | - $thead = ['Group ID', 'Name', 'Description']; |
|
30 | - CLI::table($rows, $thead); |
|
31 | - } |
|
32 | - } |
|
23 | + if (empty($rows)) |
|
24 | + { |
|
25 | + CLI::write( CLI::color("There are no groups.", 'yellow') ); |
|
26 | + } |
|
27 | + else |
|
28 | + { |
|
29 | + $thead = ['Group ID', 'Name', 'Description']; |
|
30 | + CLI::table($rows, $thead); |
|
31 | + } |
|
32 | + } |
|
33 | 33 | } |
@@ -11,48 +11,48 @@ |
||
11 | 11 | protected $name = 'auth:create_user'; |
12 | 12 | protected $description = "Adds a new user to the database."; |
13 | 13 | |
14 | - protected $usage = "auth:create_user [username] [email]"; |
|
15 | - protected $arguments = [ |
|
16 | - 'username' => "The username of the new user to create", |
|
17 | - 'email' => "The email address of the new user to create", |
|
18 | - ]; |
|
14 | + protected $usage = "auth:create_user [username] [email]"; |
|
15 | + protected $arguments = [ |
|
16 | + 'username' => "The username of the new user to create", |
|
17 | + 'email' => "The email address of the new user to create", |
|
18 | + ]; |
|
19 | 19 | |
20 | - public function run(array $params = []) |
|
20 | + public function run(array $params = []) |
|
21 | 21 | { |
22 | - // Start with the fields required for the account to be usable |
|
23 | - $row = [ |
|
24 | - 'active' => 1, |
|
25 | - 'password' => bin2hex(random_bytes(24)), |
|
26 | - ]; |
|
27 | - |
|
28 | - // Consume or prompt for username |
|
29 | - $row['username'] = array_shift($params); |
|
30 | - if (empty($row['username'])) |
|
31 | - { |
|
32 | - $row['username'] = CLI::prompt('Username', null, 'required'); |
|
33 | - } |
|
34 | - |
|
35 | - // Consume or prompt for email |
|
36 | - $row['email'] = array_shift($params); |
|
37 | - if (empty($row['email'])) |
|
38 | - { |
|
39 | - $row['email'] = CLI::prompt('Email', null, 'required'); |
|
40 | - } |
|
41 | - |
|
42 | - // Run the user through the entity and insert it |
|
43 | - $user = new User($row); |
|
44 | - |
|
45 | - $users = model(UserModel::class); |
|
46 | - if ($userId = $users->insert($user)) |
|
47 | - { |
|
48 | - CLI::write(lang('Auth.registerCLI', [$row['username'], $userId]), 'green'); |
|
49 | - } |
|
50 | - else |
|
51 | - { |
|
52 | - foreach ($users->errors() as $message) |
|
53 | - { |
|
54 | - CLI::write($message, 'red'); |
|
55 | - } |
|
56 | - } |
|
57 | - } |
|
22 | + // Start with the fields required for the account to be usable |
|
23 | + $row = [ |
|
24 | + 'active' => 1, |
|
25 | + 'password' => bin2hex(random_bytes(24)), |
|
26 | + ]; |
|
27 | + |
|
28 | + // Consume or prompt for username |
|
29 | + $row['username'] = array_shift($params); |
|
30 | + if (empty($row['username'])) |
|
31 | + { |
|
32 | + $row['username'] = CLI::prompt('Username', null, 'required'); |
|
33 | + } |
|
34 | + |
|
35 | + // Consume or prompt for email |
|
36 | + $row['email'] = array_shift($params); |
|
37 | + if (empty($row['email'])) |
|
38 | + { |
|
39 | + $row['email'] = CLI::prompt('Email', null, 'required'); |
|
40 | + } |
|
41 | + |
|
42 | + // Run the user through the entity and insert it |
|
43 | + $user = new User($row); |
|
44 | + |
|
45 | + $users = model(UserModel::class); |
|
46 | + if ($userId = $users->insert($user)) |
|
47 | + { |
|
48 | + CLI::write(lang('Auth.registerCLI', [$row['username'], $userId]), 'green'); |
|
49 | + } |
|
50 | + else |
|
51 | + { |
|
52 | + foreach ($users->errors() as $message) |
|
53 | + { |
|
54 | + CLI::write($message, 'red'); |
|
55 | + } |
|
56 | + } |
|
57 | + } |
|
58 | 58 | } |