UserModel.php 5.45 KB
Newer Older
Florian Azizian's avatar
Florian Azizian committed
1
2
3
<?php

/**
4
5
* Copyright Maarch since 2008 under license.
* See LICENSE.txt file at the root folder for more details.
Florian Azizian's avatar
Florian Azizian committed
6
7
8
9
10
11
12
13
14
15
16
* This file is part of Maarch software.
*
*/

/**
* @brief User Model
* @author dev@maarch.org
*/

namespace User\models;

Damien's avatar
Damien committed
17
use SrcCore\models\AuthenticationModel;
Damien's avatar
Damien committed
18
19
20
21
use SrcCore\models\DatabaseModel;
use SrcCore\models\ValidatorModel;

class UserModel
Florian Azizian's avatar
Florian Azizian committed
22
{
Damien's avatar
Damien committed
23
24
25
26
27
28
29
30
31
32
    public static function get(array $aArgs)
    {
        ValidatorModel::arrayType($aArgs, ['select', 'where', 'data', 'orderBy']);
        ValidatorModel::intType($aArgs, ['limit']);

        $aUsers = DatabaseModel::select([
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['users'],
            'where'     => empty($aArgs['where']) ? [] : $aArgs['where'],
            'data'      => empty($aArgs['data']) ? [] : $aArgs['data'],
Damien's avatar
Damien committed
33
            'orderBy'   => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'],
Damien's avatar
Damien committed
34
35
36
37
38
39
40
41
42
43
            'limit'     => empty($aArgs['limit']) ? 0 : $aArgs['limit']
        ]);

        return $aUsers;
    }

    public static function getById(array $aArgs)
    {
        ValidatorModel::notEmpty($aArgs, ['id']);
        ValidatorModel::intVal($aArgs, ['id']);
44
        ValidatorModel::arrayType($aArgs, ['select']);
Damien's avatar
Damien committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

        $aUser = DatabaseModel::select([
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['users'],
            'where'     => ['id = ?'],
            'data'      => [$aArgs['id']]
        ]);

        if (empty($aUser)) {
            return [];
        }

        return $aUser[0];
    }

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    public static function getByLogin(array $aArgs)
    {
        ValidatorModel::notEmpty($aArgs, ['login']);
        ValidatorModel::stringType($aArgs, ['login']);
        ValidatorModel::arrayType($aArgs, ['select']);

        $user = DatabaseModel::select([
            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
            'table'     => ['users'],
            'where'     => ['login = ?'],
            'data'      => [$aArgs['login']]
        ]);

        if (empty($user)) {
            return [];
        }

        return $user[0];
    }

Damien's avatar
Damien committed
80
    public static function create(array $args)
Florian Azizian's avatar
Florian Azizian committed
81
    {
82
        ValidatorModel::notEmpty($args, ['login', 'email', 'firstname', 'lastname', 'picture']);
Damien's avatar
Damien committed
83
        ValidatorModel::stringType($args, ['login', 'email', 'firstname', 'lastname', 'phone', 'picture', 'mode', 'signatureModes', 'x509_fingerprint']);
Damien's avatar
Damien committed
84

85
86
87
        if (empty($args['password'])) {
            $args['password'] = AuthenticationModel::generatePassword();
        }
Damien's avatar
Damien committed
88
        $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'users_id_seq']);
Florian Azizian's avatar
Florian Azizian committed
89
90
91
92

        DatabaseModel::insert([
            'table'         => 'users',
            'columnsValues' => [
Damien's avatar
Damien committed
93
94
95
                'id'                            => $nextSequenceId,
                'login'                         => $args['login'],
                'email'                         => $args['email'],
96
                'password'                      => $args['password'],
Damien's avatar
Damien committed
97
98
                'firstname'                     => $args['firstname'],
                'lastname'                      => $args['lastname'],
Damien's avatar
Damien committed
99
                'phone'                         => $args['phone'],
100
                '"isRest"'                      => empty($args['isRest']) ? 'false' : 'true',
Damien's avatar
Damien committed
101
                'picture'                       => $args['picture'],
102
                'password_modification_date'    => 'CURRENT_TIMESTAMP',
103
104
                'signature_modes'               => $args['signatureModes'],
                'x509_fingerprint'              => $args['x509_fingerprint'],
Florian Azizian's avatar
Florian Azizian committed
105
106
107
            ]
        ]);

Damien's avatar
Damien committed
108
        return $nextSequenceId;
Florian Azizian's avatar
Florian Azizian committed
109
110
    }

Damien's avatar
Damien committed
111
    public static function update(array $args)
Damien's avatar
Damien committed
112
    {
Damien's avatar
Damien committed
113
114
        ValidatorModel::notEmpty($args, ['set', 'where', 'data']);
        ValidatorModel::arrayType($args, ['set', 'where', 'data']);
Damien's avatar
Damien committed
115

Damien's avatar
Damien committed
116
        DatabaseModel::update([
Damien's avatar
Damien committed
117
118
119
120
            'table' => 'users',
            'set'   => $args['set'],
            'where' => $args['where'],
            'data'  => $args['data']
Damien's avatar
Damien committed
121
122
123
124
125
        ]);

        return true;
    }

Damien's avatar
Damien committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    public static function delete(array $args)
    {
        ValidatorModel::notEmpty($args, ['id']);
        ValidatorModel::intVal($args, ['id']);

        DatabaseModel::delete([
            'table' => 'users',
            'where' => ['id = ?'],
            'data'  => [$args['id']]
        ]);

        return true;
    }

Damien's avatar
Damien committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    public static function updatePassword(array $aArgs)
    {
        ValidatorModel::notEmpty($aArgs, ['id', 'password']);
        ValidatorModel::intVal($aArgs, ['id']);
        ValidatorModel::stringType($aArgs, ['password']);

        DatabaseModel::update([
            'table'     => 'users',
            'set'       => [
                'password'                      => AuthenticationModel::getPasswordHash($aArgs['password']),
                'password_modification_date'    => 'CURRENT_TIMESTAMP'
            ],
            'where'     => ['id = ?'],
            'data'      => [$aArgs['id']]
        ]);

        return true;
    }

Damien's avatar
Damien committed
159
160
    public static function getLabelledUserById(array $aArgs)
    {
161
        ValidatorModel::notEmpty($aArgs, ['id']);
Damien's avatar
Damien committed
162
163
        ValidatorModel::intVal($aArgs, ['id']);

164
        $rawUser = UserModel::getById(['id' => $aArgs['id'], 'select' => ['firstname', 'lastname']]);
Damien's avatar
Damien committed
165
166
167
168
169
170
171
172

        $labelledUser = '';
        if (!empty($rawUser)) {
            $labelledUser = $rawUser['firstname']. ' ' .$rawUser['lastname'];
        }

        return $labelledUser;
    }
Florian Azizian's avatar
Florian Azizian committed
173
}