Skip to content

Commit b0af78a

Browse files
author
Julien Veyssier
committed
add support for circles in approval rules
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
1 parent 4e98fd8 commit b0af78a

File tree

7 files changed

+205
-15
lines changed

7 files changed

+205
-15
lines changed

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<name>Approval</name>
55
<summary>Let users approve or disapprove files</summary>
66
<description><![CDATA[Approve/reject files based on hidden tags rules.]]></description>
7-
<version>0.0.3</version>
7+
<version>0.0.2</version>
88
<licence>agpl</licence>
99
<author>Julien Veyssier</author>
1010
<namespace>Approval</namespace>

lib/Controller/ConfigController.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
use OCP\IConfig;
1717
use OCP\IL10N;
1818
use Psr\Log\LoggerInterface;
19-
2019
use OCP\AppFramework\Http;
2120
use OCP\AppFramework\Http\RedirectResponse;
22-
2321
use OCP\AppFramework\Http\ContentSecurityPolicy;
22+
use OCP\App\IAppManager;
2423

2524
use OCP\IRequest;
2625
use OCP\AppFramework\Http\DataResponse;
@@ -39,6 +38,7 @@ public function __construct($AppName,
3938
IRequest $request,
4039
IConfig $config,
4140
IUserManager $userManager,
41+
IAppManager $appManager,
4242
IL10N $l,
4343
LoggerInterface $logger,
4444
RuleService $ruleService,
@@ -49,6 +49,7 @@ public function __construct($AppName,
4949
$this->config = $config;
5050
$this->logger = $logger;
5151
$this->userManager = $userManager;
52+
$this->appManager = $appManager;
5253
$this->ruleService = $ruleService;
5354
}
5455

@@ -57,14 +58,23 @@ public function __construct($AppName,
5758
* @return DataResponse
5859
*/
5960
public function getRules(): DataResponse {
61+
$circlesEnabled = $this->appManager->isEnabledForUser('circles');
62+
6063
$rules = $this->ruleService->getRules();
6164
foreach ($rules as $id => $rule) {
6265
foreach ($rule['who'] as $k => $elem) {
6366
if (isset($elem['userId'])) {
6467
$user = $this->userManager->get($elem['userId']);
6568
$rules[$id]['who'][$k]['displayName'] = $user ? $user->getDisplayName() : $elem['userId'];
66-
} else {
69+
} elseif (isset($elem['groupId'])) {
6770
$rules[$id]['who'][$k]['displayName'] = $elem['groupId'];
71+
} elseif (isset($elem['circleId'])) {
72+
if ($circlesEnabled) {
73+
$circleDetails = \OCA\Circles\Api\v1\Circles::detailsCircle($elem['circleId']);
74+
$rules[$id]['who'][$k]['displayName'] = $circleDetails->getName();
75+
} else {
76+
unset($rules[$id]['who'][$k]);
77+
}
6878
}
6979
}
7080
}

lib/Migration/Version000002Date20210330142733.php renamed to lib/Migration/Version000003Date20210331142733.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Auto-generated migration step: Please modify to your needs!
1414
*/
15-
class Version000002Date20210330142733 extends SimpleMigrationStep {
15+
class Version000003Date20210331142733 extends SimpleMigrationStep {
1616

1717
/**
1818
* @param IOutput $output
@@ -90,6 +90,24 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
9090
$table->setPrimaryKey(['id']);
9191
}
9292

93+
if (!$schema->hasTable('approval_rule_circles')) {
94+
$table = $schema->createTable('approval_rule_circles');
95+
$table->addColumn('id', 'integer', [
96+
'autoincrement' => true,
97+
'notnull' => true,
98+
'length' => 4,
99+
]);
100+
$table->addColumn('rule_id', 'integer', [
101+
'notnull' => true,
102+
'length' => 4,
103+
]);
104+
$table->addColumn('circle_id', 'string', [
105+
'notnull' => true,
106+
'length' => 300,
107+
]);
108+
$table->setPrimaryKey(['id']);
109+
}
110+
93111
return $schema;
94112
}
95113

lib/Service/ApprovalService.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use OCP\IUserManager;
2424
use OCP\IUser;
2525
use OCP\IGroupManager;
26+
use OCP\App\IAppManager;
2627
use OCP\Notification\IManager as INotificationManager;
2728

2829
use OCA\Approval\AppInfo\Application;
@@ -44,6 +45,7 @@ public function __construct (string $appName,
4445
IRootFolder $root,
4546
IUserManager $userManager,
4647
IGroupManager $groupManager,
48+
IAppManager $appManager,
4749
INotificationManager $notificationManager,
4850
RuleService $ruleService,
4951
ActivityManager $activityManager,
@@ -55,6 +57,7 @@ public function __construct (string $appName,
5557
$this->root = $root;
5658
$this->userManager = $userManager;
5759
$this->groupManager = $groupManager;
60+
$this->appManager = $appManager;
5861
$this->notificationManager = $notificationManager;
5962
$this->activityManager = $activityManager;
6063
$this->tagManager = $tagManager;
@@ -86,6 +89,8 @@ private function userHasAccessTo(int $fileId, ?string $userId): bool {
8689
}
8790

8891
private function userIsAuthorizedByRule(string $userId, array $rule): bool {
92+
$circlesEnabled = $this->appManager->isEnabledForUser('circles');
93+
8994
$user = $this->userManager->get($userId);
9095

9196
$ruleUserIds = array_map(function ($w) {
@@ -109,6 +114,42 @@ private function userIsAuthorizedByRule(string $userId, array $rule): bool {
109114
return true;
110115
}
111116
}
117+
// if user is member of one rule's circle list
118+
if ($circlesEnabled) {
119+
$ruleCircleIds = array_map(function ($w) {
120+
return $w['circleId'];
121+
}, array_filter($rule['who'], function ($w) {
122+
return isset($w['circleId']);
123+
}));
124+
foreach ($ruleCircleIds as $circleId) {
125+
if ($this->isUserInCircle($userId, $circleId)) {
126+
return true;
127+
}
128+
}
129+
}
130+
}
131+
return false;
132+
}
133+
134+
private function isUserInCircle(string $userId, string $circleId): bool {
135+
$circleDetails = null;
136+
try {
137+
$circleDetails = \OCA\Circles\Api\v1\Circles::detailsCircle($circleId);
138+
} catch (\OCA\Circles\Exceptions\CircleDoesNotExistException $e) {
139+
return false;
140+
}
141+
// is the circle owner
142+
if ($circleDetails->getOwner()->getUserId() === $userId) {
143+
return true;
144+
} else {
145+
if ($circleDetails->getMembers() !== null) {
146+
foreach ($circleDetails->getMembers() as $m) {
147+
// is member of this circle
148+
if ($m->getUserId() === $userId) {
149+
return true;
150+
}
151+
}
152+
}
112153
}
113154
return false;
114155
}

lib/Service/RuleService.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,53 @@ public function saveRule(int $id, int $tagPending, int $tagApproved, int $tagRej
216216
$qb = $qb->resetQueryParts();
217217
}
218218

219+
// circles
220+
$toDelete = [];
221+
$toAdd = [];
222+
$oldCircleIds = [];
223+
foreach ($rule['who'] as $elem) {
224+
if (isset($elem['circleId'])) {
225+
$oldCircleIds[] = $elem['circleId'];
226+
}
227+
}
228+
$newCircleIds = [];
229+
foreach ($who as $elem) {
230+
if (isset($elem['circleId'])) {
231+
$newCircleIds[] = $elem['circleId'];
232+
}
233+
}
234+
235+
foreach ($oldCircleIds as $gid) {
236+
if (!in_array($gid, $newCircleIds)) {
237+
$toDelete[] = $gid;
238+
}
239+
}
240+
foreach ($newCircleIds as $gid) {
241+
if (!in_array($gid, $oldCircleIds)) {
242+
$toAdd[] = $gid;
243+
}
244+
}
245+
foreach ($toDelete as $gid) {
246+
$qb->delete('approval_rule_circles')
247+
->where(
248+
$qb->expr()->eq('rule_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
249+
)
250+
->andWhere(
251+
$qb->expr()->eq('circle_id', $qb->createNamedParameter($gid, IQueryBuilder::PARAM_STR))
252+
);
253+
$req = $qb->execute();
254+
$qb = $qb->resetQueryParts();
255+
}
256+
foreach ($toAdd as $gid) {
257+
$qb->insert('approval_rule_circles')
258+
->values([
259+
'circle_id' => $qb->createNamedParameter($gid, IQueryBuilder::PARAM_STR),
260+
'rule_id' => $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
261+
]);
262+
$req = $qb->execute();
263+
$qb = $qb->resetQueryParts();
264+
}
265+
219266
return ['id' => $id];
220267
}
221268

@@ -266,6 +313,14 @@ public function createRule(int $tagPending, int $tagApproved, int $tagRejected,
266313
]);
267314
$req = $qb->execute();
268315
$qb = $qb->resetQueryParts();
316+
} elseif (isset($elem['circleId'])) {
317+
$qb->insert('approval_rule_circles')
318+
->values([
319+
'circle_id' => $qb->createNamedParameter($elem['circleId'], IQueryBuilder::PARAM_STR),
320+
'rule_id' => $qb->createNamedParameter($insertedRuleId, IQueryBuilder::PARAM_INT),
321+
]);
322+
$req = $qb->execute();
323+
$qb = $qb->resetQueryParts();
269324
}
270325
}
271326

@@ -357,6 +412,7 @@ public function getRule(int $id): ?array {
357412
$req->closeCursor();
358413
$qb = $qb->resetQueryParts();
359414

415+
// groups
360416
$qb->select('*')
361417
->from('approval_rule_groups')
362418
->where(
@@ -371,6 +427,21 @@ public function getRule(int $id): ?array {
371427
$req->closeCursor();
372428
$qb = $qb->resetQueryParts();
373429

430+
// circles
431+
$qb->select('*')
432+
->from('approval_rule_circles')
433+
->where(
434+
$qb->expr()->eq('rule_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
435+
);
436+
$req = $qb->execute();
437+
while ($row = $req->fetch()) {
438+
$rule['who'][] = [
439+
'circleId' => $row['circle_id']
440+
];
441+
}
442+
$req->closeCursor();
443+
$qb = $qb->resetQueryParts();
444+
374445
return $rule;
375446
}
376447

@@ -416,6 +487,7 @@ public function getRules(): array {
416487
$req->closeCursor();
417488
$qb = $qb->resetQueryParts();
418489

490+
// groups
419491
$qb->select('*')
420492
->from('approval_rule_groups')
421493
->where(
@@ -429,6 +501,21 @@ public function getRules(): array {
429501
}
430502
$req->closeCursor();
431503
$qb = $qb->resetQueryParts();
504+
505+
// circles
506+
$qb->select('*')
507+
->from('approval_rule_circles')
508+
->where(
509+
$qb->expr()->eq('rule_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
510+
);
511+
$req = $qb->execute();
512+
while ($row = $req->fetch()) {
513+
$rules[$id]['who'][] = [
514+
'circleId' => $row['circle_id']
515+
];
516+
}
517+
$req->closeCursor();
518+
$qb = $qb->resetQueryParts();
432519
}
433520

434521
return $rules;

src/components/AdminSettings.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ export default {
9696
this.rules[id].who = this.rules[id].who.map(w => {
9797
return {
9898
...w,
99-
trackKey: w.userId ? 'user-' + w.userId : 'group-' + w.groupId,
99+
trackKey: w.userId
100+
? 'user-' + w.userId
101+
: w.groupId
102+
? 'group-' + w.groupId
103+
: 'circle-' + w.circleId,
100104
}
101105
})
102106
}
@@ -120,6 +124,7 @@ export default {
120124
return {
121125
userId: u.userId,
122126
groupId: u.groupId,
127+
circleId: u.circleId,
123128
}
124129
}),
125130
}
@@ -160,6 +165,7 @@ export default {
160165
return {
161166
userId: u.userId,
162167
groupId: u.groupId,
168+
circleId: u.circleId,
163169
}
164170
}),
165171
}

0 commit comments

Comments
 (0)