forked from agiliway/com.agiliway.custompermission
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustompermission.php
251 lines (217 loc) · 7.35 KB
/
custompermission.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
require_once 'custompermission.civix.php';
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function custompermission_civicrm_config(&$config) {
_custompermission_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function custompermission_civicrm_install() {
_custompermission_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function custompermission_civicrm_postInstall() {
_custompermission_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function custompermission_civicrm_uninstall() {
_custompermission_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function custompermission_civicrm_enable() {
_custompermission_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function custompermission_civicrm_disable() {
_custompermission_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function custompermission_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _custompermission_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_buildForm().
*
* Sets fields and a default value for attachment.
*
* @param $formName
* @param $form
*/
function custompermission_civicrm_buildForm($formName, &$form) {
if ($formName == 'CRM_Admin_Form_RelationshipType') {
$form->addRadio('is_permission_a_b', ts('Permission for Contact A to Contact B'), CRM_Core_SelectValues::getPermissionedRelationshipOptions(), ['required' => TRUE]);
$form->addRadio('is_permission_b_a', ts('Permission for Contact B to Contact A'), CRM_Core_SelectValues::getPermissionedRelationshipOptions(), ['required' => TRUE]);
$params = ['relationship_type_id' => $form->getVar('_id')];
$defaults = [];
CRM_Custompermission_BAO_RelationshipTypeSetting::retrieve($params, $defaults);
$form->setDefaults($defaults);
CRM_Core_Region::instance('page-body')->add([
'template' => CRM_Custompermission_ExtensionUtil::path() . '/templates/CRM/Custompermission/Form/Field/RelationshipType.tpl',
]);
}
if ($formName == 'CRM_Contact_Form_Relationship') {
if ($form->elementExists('is_permission_a_b')) {
$form->removeElement('is_permission_a_b');
}
if ($form->elementExists('is_permission_b_a')) {
$form->removeElement('is_permission_b_a');
}
}
if ($formName == 'CRM_Note_Form_Note') {
if (!$form->elementExists('entryURL')) {
$currentUrl = (CRM_Utils_System::isSSL() ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$form->addElement('hidden', 'entryURL', $currentUrl);
}
}
}
/**
* Implements hook_civicrm_postProcess().
*
* @param $formName
* @param $form
*/
function custompermission_civicrm_postProcess($formName, &$form) {
if ($formName == 'CRM_Admin_Form_RelationshipType') {
$params = $form->exportValues();
$id = $form->getVar('_id');
if (!$id) {
try {
$relTypeId = civicrm_api3('RelationshipType', 'getvalue', [
'return' => 'id',
'label_a_b' => $params['label_a_b'],
]);
} catch (CiviCRM_API3_Exception $e) {
return;
}
$id = (int) $relTypeId;
}
$params['relationship_type_id'] = $id;
CRM_Custompermission_BAO_RelationshipTypeSetting::add($params);
}
}
/**
* @param array $permission
* @param bool $granted
*
* @throws \CRM_Core_Exception
*/
function custompermission_civicrm_permission_check($permission, &$granted) {
if (!$granted) {
$cid = CRM_Utils_Request::retrieve('cid', 'Positive');
if ($cid == CRM_Core_Session::singleton()->get('userID')) {
if ($permission == 'view all contacts' && CRM_Utils_System::getUrlPath() == 'civicrm/ajax/contactrelationships') {
$granted = CRM_Core_Permission::check('view_my_relationships');
}
}
else {
if ($permission == 'view all contacts') {
$granted = CRM_Custompermission_RelationshipsChecker::checkContactAccess($cid, CRM_Contact_BAO_Relationship::VIEW);
}
if ($permission == 'edit all contacts') {
$granted = CRM_Custompermission_RelationshipsChecker::checkContactAccess($cid, CRM_Contact_BAO_Relationship::EDIT);
}
if ($permission == 'access all cases and activities') {
if (CRM_Custompermission_Helper::accessUserToContact($cid, CRM_Contact_BAO_Relationship::VIEW)) {
$granted = TRUE;
}
}
}
if ($permission == 'view all contacts' && CRM_Core_Permission::check('view all contacts in domain')) {
$granted = TRUE;
}
elseif ($permission == 'edit all contacts' && CRM_Core_Permission::check('edit all contacts in domain')) {
$granted = TRUE;
}
}
}
/**
* Add new type of permission
*/
function custompermission_civicrm_permission(&$permissions) {
$prefix = ts('CiviCRM') . ': ';
$permissions += [
'view_my_relationships' => $prefix . ts('View my relationships'),
];
}
/**
* Implements hook_civicrm_aclWhereClause
*
* @param $type
* @param $tables
* @param $whereTables
* @param $contactID
* @param $where
*/
function custompermission_civicrm_aclWhereClause($type, &$tables, &$whereTables, &$contactID, &$where) {
if (!$contactID) {
return;
}
$urlPath = CRM_Utils_System::getUrlPath();
if (!($urlPath == 'civicrm/contact/search' || $urlPath == 'civicrm/contact/search/advanced')) {
return;
}
$hierarchyMainOrg = Civi::settings()->get('hierarchy_main_organization');
if (!$hierarchyMainOrg) {
return;
}
$userID = CRM_Core_Session::getLoggedInContactID();
$helper = new CRM_Custompermission_Helper;
$helper::$chainRelationship = [];
$includeContacts = [0];
$mainOrgChildren = $helper::getHierarchyAllChildren($hierarchyMainOrg);
$helper::chainRelationship($mainOrgChildren['children'], $userID, TRUE);
if (empty($helper::$chainRelationship)) {
return;
}
foreach ($helper::$chainRelationship as $contact) {
$includeContacts[] = $contact['id'];
foreach ($contact['children'] as $child) {
$includeContacts[] = $child['id'];
}
}
$userChildren = $helper::getHierarchyAllChildren($userID);
$helper::$chainRelationship = [];
$helper::getAllChildrenByHierarchy($userChildren['children']);
$includeContacts = array_merge($includeContacts, $helper::$chainRelationship);
$includeContacts[] = $hierarchyMainOrg;
foreach ($mainOrgChildren['children']['contacts'] as $contact) {
$includeContacts[] = $contact['id'];
}
$includeContacts = array_unique($includeContacts);
$where .= 'contact_a.id in (' . implode(',', $includeContacts) . ')';
}
// /**
// * Implements hook_civicrm_entityTypes().
// *
// * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
// */
// function custompermission_civicrm_entityTypes(&$entityTypes) {
// _custompermission_civix_civicrm_entityTypes($entityTypes);
// }