summaryrefslogtreecommitdiff
path: root/app/Api/User.php
blob: 7409a41bb852a266dfb5ccd44dd2b0b186b348ff (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
<?php

namespace Api;

use Auth\Ldap;

/**
 * User API controller
 *
 * @package  api
 * @author   Frederic Guillot
 */
class User extends \Core\Base
{
    public function getUser($user_id)
    {
        return $this->user->getById($user_id);
    }

    public function getAllUsers()
    {
        return $this->user->getAll();
    }

    public function removeUser($user_id)
    {
        return $this->user->remove($user_id);
    }

    public function createUser($username, $password, $name = '', $email = '', $is_admin = 0)
    {
        $values = array(
            'username' => $username,
            'password' => $password,
            'confirmation' => $password,
            'name' => $name,
            'email' => $email,
            'is_admin' => $is_admin,
        );

        list($valid,) = $this->user->validateCreation($values);

        return $valid ? $this->user->create($values) : false;
    }

    public function createLdapUser($username = '', $email = '', $is_admin = 0)
    {
        $ldap = new Ldap($this->container);
        $user = $ldap->lookup($username, $email);

        if (! $user) {
            return false;
        }

        $values = array(
            'username' => $user['username'],
            'name' => $user['name'],
            'email' => $user['email'],
            'is_ldap_user' => 1,
            'is_admin' => $is_admin,
        );

        return $this->user->create($values);
    }

    public function updateUser($id, $username = null, $name = null, $email = null, $is_admin = null)
    {
        $values = array(
            'id' => $id,
            'username' => $username,
            'name' => $name,
            'email' => $email,
            'is_admin' => $is_admin,
        );

        foreach ($values as $key => $value) {
            if (is_null($value)) {
                unset($values[$key]);
            }
        }

        list($valid,) = $this->user->validateApiModification($values);
        return $valid && $this->user->update($values);
    }
}