1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organization\services; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User as UserElement; |
13
|
|
|
use flipbox\organization\elements\Organization as OrganizationElement; |
14
|
|
|
use yii\base\Component; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class Permission extends Component |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param UserElement $userElement |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
public function canCreateOrganization(UserElement $userElement) |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
return $userElement->admin || Craft::$app->getUserPermissions()->doesUserHavePermission( |
31
|
|
|
$userElement->id, |
32
|
|
|
'_createOrganization' |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param OrganizationElement $organizationElement |
38
|
|
|
* @param UserElement|null $userElement |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function canUpdateOrganization(UserElement $userElement, OrganizationElement $organizationElement) |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
return $userElement->admin || |
45
|
|
|
$organizationElement->isOwner($userElement) || |
46
|
|
|
( |
47
|
|
|
$organizationElement->isUser($userElement) && |
48
|
|
|
Craft::$app->getUserPermissions()->doesUserHavePermission( |
49
|
|
|
$userElement->id, |
50
|
|
|
'updateMyOrganization' |
51
|
|
|
) |
52
|
|
|
) || |
53
|
|
|
Craft::$app->getUserPermissions()->doesUserHavePermission( |
54
|
|
|
$userElement->id, |
55
|
|
|
'updateAnyOrganization' |
56
|
|
|
) || |
57
|
|
|
Craft::$app->getUserPermissions()->doesUserHavePermission( |
58
|
|
|
$userElement->id, |
59
|
|
|
'updateOrganization:' . $organizationElement->id |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determine whether a user can manage organization types |
65
|
|
|
* |
66
|
|
|
* @param UserElement $userElement |
67
|
|
|
* @param OrganizationElement $organizationElement |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function canManageOrganizationTypes(UserElement $userElement, OrganizationElement $organizationElement) |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
// Admin -or- Owner -or- [User and Permission] |
74
|
|
|
return $userElement->admin || |
75
|
|
|
$organizationElement->isOwner($userElement) || |
76
|
|
|
($organizationElement->isUser($userElement) && |
77
|
|
|
Craft::$app->getUserPermissions()->doesUserHavePermission( |
78
|
|
|
$userElement->id, |
79
|
|
|
'manageTypeAssociations' |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Determine whether a user can manage organization types |
86
|
|
|
* |
87
|
|
|
* @param UserElement $userElement |
88
|
|
|
* @param OrganizationElement $organizationElement |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function canManageOrganizationUsers(UserElement $userElement, OrganizationElement $organizationElement) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
// Admin -or- Owner -or- [User and Permission] |
95
|
|
|
return $userElement->admin || |
96
|
|
|
$organizationElement->isOwner($userElement) || |
97
|
|
|
($organizationElement->isUser($userElement) && |
98
|
|
|
Craft::$app->getUserPermissions()->doesUserHavePermission( |
99
|
|
|
$userElement->id, |
100
|
|
|
'manageUserAssociations' |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|