-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserModel.php
116 lines (102 loc) · 3.44 KB
/
UserModel.php
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
<?php
namespace App\Models;
use App\Entities\User;
use Faker\Generator;
use Myth\Auth\Entities\User as MythUser;
use Myth\Auth\Models\UserModel as MythModel;
use stdClass;
class UserModel extends MythModel
{
use CompiledRowsTrait;
protected $table = 'users';
protected $primaryKey = 'id';
protected $returnType = User::class;
protected $afterInsert = ['clearCompiledRows'];
protected $afterUpdate = ['clearCompiledRows'];
protected $afterDelete = ['clearCompiledRows'];
/**
* Add the extended properties.
*/
protected function initialize()
{
// Merge properties with parent
$this->allowedFields = array_merge($this->allowedFields, ['firstname', 'lastname', 'balance']);
$this->validationRules = array_merge($this->validationRules, [
'firstname' => 'required|string',
'lastname' => 'required|string',
'balance' => 'permit_empty|integer',
]);
}
/**
* Returns the IDs of all Users considered "staff".
*
* @return int[]
*/
public function findStaffIds(): array
{
if (! $permission = model(PermissionModel::class)->where('name', 'manageAny')->first()) {
return [];
}
$ids = $this->builder()
->select('users.id')
->join('auth_groups_users', 'users.id = auth_groups_users.user_id', 'left')
->join('auth_groups_permissions', 'auth_groups_permissions.group_id = auth_groups_users.group_id', 'left')
->where('auth_groups_permissions.permission_id', $permission->id)
->get()->getResultArray();
return array_column($ids, 'id');
}
/**
* Returns groups for a single user. Uses Myth:Auth's
* GroupModel but converts the result to objects.
*
* @param int|string|null $userId
*
* @return stdClass[] Array of group objects
*/
public function groups($userId = null): array
{
if (null === $userId) {
return [];
}
if ($result = model(GroupModel::class)->getGroupsForUser($userId)) {
// Convert the arrays to objects
$result = array_map(static function ($row) {
// Use the Group ID for the primary ID
$row['id'] = $row['group_id'];
return (object) $row;
}, $result);
}
return $result;
}
/**
* Fetch or build the compiled rows for browsing,
* applying filters, and sorting.
*
* @return array[]
*/
protected function fetchCompiledRows(): array
{
return $this->builder()
->select('users.*, auth_groups.id AS group_id, auth_groups.name as group')
->join('auth_groups_users', 'users.id = auth_groups_users.user_id', 'left')
->join('auth_groups', 'auth_groups_users.group_id = auth_groups.id', 'left')
->get()->getResultArray();
}
/**
* Faked data for Fabricator.
*
* @return User
*/
public function fake(Generator &$faker): MythUser
{
return new User([
'email' => $faker->email,
'username' => $faker->userName,
'firstname' => $faker->firstName,
'lastname' => $faker->lastName,
'password' => bin2hex(random_bytes(24)),
'balance' => random_int(0, 1) ? random_int(100, 5000) : 0,
'active' => random_int(0, 20) ? 1 : 0,
]);
}
}