Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UserRepository.php « Repository « lib - github.com/nextcloud/user_sql.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f3e3fbbfd7c9af64fd9490e953e05c96c9065739 (plain)
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
<?php
/**
 * Nextcloud - user_sql
 *
 * @copyright 2020 Marcin Łojewski <dev@mlojewski.me>
 * @author    Marcin Łojewski <dev@mlojewski.me>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace OCA\UserSQL\Repository;

use OCA\UserSQL\Constant\Query;
use OCA\UserSQL\Model\User;
use OCA\UserSQL\Query\DataQuery;

/**
 * The user repository.
 *
 * @author Marcin Łojewski <dev@mlojewski.me>
 */
class UserRepository
{
    const DISPLAY_NAME_FIELD = 0b0001;
    const EMAIL_FIELD = 0b0010;
    const PASSWORD_FIELD = 0b0100;
    const QUOTA_FIELD = 0b1000;

    /**
     * @var DataQuery The data query object.
     */
    private $dataQuery;

    /**
     * The class constructor.
     *
     * @param DataQuery $dataQuery The data query object.
     */
    public function __construct(DataQuery $dataQuery)
    {
        $this->dataQuery = $dataQuery;
    }

    /**
     * Get an user entity object.
     *
     * @param string $uid           The user ID.
     * @param bool   $caseSensitive TRUE for case sensitive search,
     *                              FALSE for case insensitive search.
     *
     * @return User The user entity, NULL if it does not exists or
     *              FALSE on failure.
     */
    public function findByUid($uid, $caseSensitive = true)
    {
        if ($caseSensitive) {
            return $this->dataQuery->queryEntity(
                Query::FIND_USER, User::class, [Query::UID_PARAM => $uid]
            );
        } else {
            return $this->dataQuery->queryEntity(
                Query::FIND_USER_CASE_INSENSITIVE, User::class, [Query::UID_PARAM => $uid]
            );
        }
    }

    /**
     * Get an user entity object.
     *
     * @param string $query         The user ID or email address.
     * @param bool   $caseSensitive TRUE for case sensitive search,
     *                              FALSE for case insensitive search.
     *
     * @return User The user entity, NULL if it does not exists or
     *              FALSE on failure.
     */
    public function findByUidOrEmail($query, $caseSensitive = true)
    {
        if ($caseSensitive) {
            return $this->dataQuery->queryEntity(
                Query::FIND_USER_BY_UID_OR_EMAIL, User::class,
                [Query::UID_PARAM => $query, Query::EMAIL_PARAM => $query]
            );
        } else {
            return $this->dataQuery->queryEntity(
                Query::FIND_USER_BY_UID_OR_EMAIL_CASE_INSENSITIVE, User::class,
                [Query::UID_PARAM => $query, Query::EMAIL_PARAM => $query]
            );
        }
    }

    /**
     * Get an array of user entity objects.
     *
     * @param string $search The search term. Defaults to "" (empty string).
     * @param int    $limit  (optional) Results limit.
     *                       Defaults to -1 (no limit).
     * @param int    $offset (optional) Results offset. Defaults to 0.
     *
     * @return User[] Array of user entity objects or FALSE on failure.
     */
    public function findAllBySearchTerm($search = "", $limit = -1, $offset = 0)
    {
        return $this->dataQuery->queryEntities(
            Query::FIND_USERS, User::class, [Query::SEARCH_PARAM => $search],
            $limit, $offset
        );
    }

    /**
     * Get the number of users.
     *
     * @param string $search The search term. Defaults to "" (empty string).
     *
     * @return int The number of users or FALSE on failure.
     */
    public function countAll($search = "")
    {
        return $this->dataQuery->queryValue(
            Query::COUNT_USERS, [Query::SEARCH_PARAM => $search]
        );
    }

    /**
     * Save an user entity object.
     *
     * @param User $user   The user entity.
     * @param int  $fields Fields to update.
     *
     * @return bool TRUE on success, FALSE otherwise.
     */
    public function save($user, $fields)
    {
        $status = true;

        if ($fields & self::DISPLAY_NAME_FIELD) {
            $status =& $this->dataQuery->update(
                Query::UPDATE_DISPLAY_NAME, [
                    Query::NAME_PARAM => $user->name,
                    Query::UID_PARAM => $user->uid
                ]
            );
        }
        if ($fields & self::PASSWORD_FIELD) {
            $status =& $this->dataQuery->update(
                Query::UPDATE_PASSWORD, [
                    Query::PASSWORD_PARAM => $user->password,
                    Query::UID_PARAM => $user->uid
                ]
            );
        }
        if ($fields & self::EMAIL_FIELD) {
            $status =& $this->dataQuery->update(
                Query::UPDATE_EMAIL, [
                    Query::EMAIL_PARAM => $user->email,
                    Query::UID_PARAM => $user->uid
                ]
            );
        }
        if ($fields & self::QUOTA_FIELD) {
            $status =& $this->dataQuery->update(
                Query::UPDATE_QUOTA, [
                    Query::QUOTA_PARAM => $user->quota,
                    Query::UID_PARAM => $user->uid
                ]
            );
        }

        return $status;
    }
}