Completed
Pull Request — master (#4297)
by Craig
06:27
created

requirePasswordChangeByGroup()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.5555
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ZAuthModule\Helper;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Doctrine\Persistence\ObjectManager;
18
use Zikula\GroupsModule\Entity\RepositoryInterface\GroupRepositoryInterface;
19
use Zikula\UsersModule\Api\ApiInterface\CurrentUserApiInterface;
20
use Zikula\UsersModule\Constant;
21
use Zikula\ZAuthModule\ZAuthConstant;
22
23
class BatchPasswordChangeHelper
24
{
25
    /**
26
     * @var CurrentUserApiInterface
27
     */
28
    private $currentUserApi;
29
30
    /**
31
     * @var ObjectManager
32
     */
33
    private $manager;
34
35
    /**
36
     * @var GroupRepositoryInterface
37
     */
38
    private $groupRepository;
39
40
    public function __construct(
41
        CurrentUserApiInterface $currentUserApi,
42
        ManagerRegistry $managerRegistry,
43
        GroupRepositoryInterface $groupRepository
44
    ) {
45
        $this->currentUserApi = $currentUserApi;
46
        $this->manager = $managerRegistry->getManager();
47
        $this->groupRepository = $groupRepository;
48
    }
49
50
    public function requirePasswordChangeByGroup(int $groupId): int
51
    {
52
        $group = $this->groupRepository->find($groupId);
53
        $currentUid = $this->currentUserApi->get('uid');
54
        $count = 0;
55
56
        /** @var \Zikula\UsersModule\Entity\UserEntity $user */
57
        foreach ($group->getUsers() as $user) {
58
            $authMethod = $user->getAttributeValue('authenticationMethod');
59
            $uid = $user->getUid();
60
            if ((Constant::USER_ID_ANONYMOUS === $uid) || ($currentUid === $uid) || ('native_' !== mb_substr($authMethod, 0, 7))) {
61
                continue;
62
            }
63
            $user->setAttribute(ZAuthConstant::REQUIRE_PASSWORD_CHANGE_KEY, true);
64
            $count++;
65
        }
66
        $this->manager->flush();
67
68
        return $count;
69
    }
70
}
71