Passed
Push — shared-storage-experiments ( 606554...2addb2 )
by Matias
08:21
created

DisabledUserRemovalTask::description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019, Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\FaceRecognition\BackgroundJob\Tasks;
24
25
use OCP\IConfig;
26
use OCP\IUser;
27
28
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
29
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
30
use OCA\FaceRecognition\Db\ImageMapper;
31
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
32
33
use OCA\FaceRecognition\FaceManagementService;
34
35
/**
36
 * Task that, for each user, check if disabled the analysis,
37
 * and if necessary remove data from this application
38
 */
39
class DisabledUserRemovalTask extends FaceRecognitionBackgroundTask {
40
41
	/** @var IConfig Config */
42
	private $config;
43
44
	/** @var ImageMapper Image mapper */
45
	private $imageMapper;
46
47
	/** @var FaceManagementService */
48
	protected $faceManagementService;
49
50
	/**
51
	 * @param IConfig $config Config
52
	 * @param ImageMapper $imageMapper Image mapper
53
	 * @param FaceManagementService $faceManagementService
54
	 */
55
	public function __construct (IConfig               $config,
56
		                     ImageMapper           $imageMapper,
57
		                     FaceManagementService $faceManagementService)
58
	{
59
		parent::__construct();
60
		$this->config                = $config;
61
		$this->imageMapper           = $imageMapper;
62
		$this->faceManagementService = $faceManagementService;
63
	}
64
65
	/**
66
	 * @inheritdoc
67
	 */
68
	public function description() {
69
		return "Purge all the information of a user when disable the analysis.";
70
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75
	public function execute(FaceRecognitionContext $context) {
76
		$this->setContext($context);
77
78
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
79
80
		// Check if we are called for one user only, or for all user in instance.
81
		$eligable_users = array();
82
		if (is_null($this->context->user)) {
83
			$this->context->userManager->callForSeenUsers(function (IUser $user) use (&$eligable_users) {
84
				$eligable_users[] = $user->getUID();
85
			});
86
		} else {
87
			$eligable_users[] = $this->context->user->getUID();
88
		}
89
90
		// Reset user datas if needed.
91
		foreach($eligable_users as $userId) {
92
			$userEnabled = $this->config->getUserValue($userId, 'facerecognition', 'enabled', 'false');
93
			$imageCount = $this->imageMapper->countUserImages($userId, $model);
94
			if ($userEnabled === 'false' && $imageCount > 0) {
95
				// TODO: Check that the user really has information to remove.
96
				$this->logInfo(sprintf('Removing data from user %s that disable analysis', $userId));
97
				$this->faceManagementService->resetAllForUser($userId);
98
			}
99
			yield;
100
		}
101
102
		return true;
103
	}
104
105
}
106