UserCreateUpdateDelete.php 3.8 KB
Newer Older
1
<?php
2

3
namespace T3o\T3oLdap\Utility;
4

5
6
7
8
9
10
11
12
13
14
15
/*
 * (c) 2016 by mehrwert intermediale kommunikation GmbH
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 */

16
17
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
19
20
21
22
23
24

/**
 * Class to create, update or delete accounts in LDAP
 *
 * @since 1.0.0
 */
25
26
class UserCreateUpdateDelete
{
27
28
29
30

    /**
     * Update a LDAP User. If the user does not exist, it will be created.
     *
31
     * @param \In2code\Femanager\Domain\Model\User $user The frontend user
32
     * @param boolean $createIfNotExists Create the user if it does not exist
33
     * @param string $updatePassword
34
35
     * @return boolean
     */
36
    public function updateUser(\In2code\Femanager\Domain\Model\User $user, $createIfNotExists = true, $updatePassword = '')
37
    {
38
39
40

        $ret = false;

41
42
        /** @var \T3o\T3oLdap\Connectors\Ldap $ldap */
        $ldap = GeneralUtility::makeInstance(\T3o\T3oLdap\Connectors\Ldap::class);
43
        $feUserUid = $user->getUid();
44

45
        if ($ldap->userExists($user->getUsername())) {
46

47
            $ret = $ldap->updateUser($user);
48

49
            if ($ret === true) {
50
51
                /** @var $flashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
                $flashMessage = GeneralUtility::makeInstance(FlashMessage::class,
52
                    'Frontend user ' . $user->getUsername() . ' (UID ' . $feUserUid . ') has been updated in LDAP.',
53
                    'LDAP Update User Status', FlashMessage::OK);
54
            } else {
55
56
                /** @var $flashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
                $flashMessage = GeneralUtility::makeInstance(FlashMessage::class,
57
                    'Failed to update frontend user ' . $user->getUsername() . ' (UID ' . $feUserUid . ') in LDAP. ' . 'The server responded with: ' . $ldap->getLastLdapError(),
58
                    'LDAP Update User Status', FlashMessage::ERROR);
59
60
            }

61
62
63
64
            // TODO Delete User in LDAP (notify consumer systems)
            // $ret = $ldap->deleteUser($userData);

        } elseif ($createIfNotExists === true) {
65

66
            $ret = $ldap->createUser($user);
67
68

            if ($ret === true) {
69
                /** @var $flashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
70
                $flashMessage = GeneralUtility::makeInstance(FlashMessage::class,
71
                    'Frontend user ' . $user->getUsername() . ' (UID ' . $feUserUid . ') has been created in LDAP.',
72
                    'LDAP Create User Status', FlashMessage::OK);
73
            } else {
74
                /** @var $flashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
75
                $flashMessage = GeneralUtility::makeInstance(FlashMessage::class,
76
                    'Failed to update frontend user ' . $user->getUsername() . ' (UID ' . $feUserUid . ') in LDAP. ' . 'The server responded with: ' . $ldap->getLastLdapError(),
77
                    'LDAP Create User Status', FlashMessage::ERROR);
78
79
80
            }
        }

81
        // if a user was created or updated $ret is true and if a new password is requested, the password will be set
82
        if ($ret === true && $updatePassword !== '') {
83
            $passwordUpdate = GeneralUtility::makeInstance(PasswordUpdate::class);
84
            $passwordUpdate->updatePassword($user->getUsername(), $updatePassword);
85
86
        }

87
        if (isset($flashMessage)) {
88
            $flashMessageQueue = GeneralUtility::makeInstance(FlashMessageQueue::class, 'core.template.flashMessages');
Thomas Löffler's avatar
Thomas Löffler committed
89
            $flashMessageQueue->addMessage($flashMessage);
90
91
92
93
94
95
        }

        return $ret;

    }

96
}