summaryrefslogtreecommitdiff
path: root/controllers/user.php
blob: 10d3ad21102c977d87b4fcc492a3b4ee56d81d9f (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

namespace Controller;

class User extends Base
{
    // Display access forbidden page
    public function forbidden()
    {
        $this->response->html($this->template->layout('user_forbidden', array(
            'menu' => 'users',
            'title' => t('Access Forbidden')
        )));
    }

    // Logout and destroy session
    public function logout()
    {
        $this->session->close();
        $this->response->redirect('?controller=user&action=login');
    }

    // Display the form login
    public function login()
    {
        if (isset($_SESSION['user'])) $this->response->redirect('?controller=app');

        $this->response->html($this->template->layout('user_login', array(
            'errors' => array(),
            'values' => array(),
            'no_layout' => true,
            'title' => t('Login')
        )));
    }

    // Check credentials
    public function check()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->user->validateLogin($values);

        if ($valid) $this->response->redirect('?controller=app');

        $this->response->html($this->template->layout('user_login', array(
            'errors' => $errors,
            'values' => $values,
            'no_layout' => true,
            'title' => t('Login')
        )));
    }

    // List all users
    public function index()
    {
        $users = $this->user->getAll();
        $nb_users = count($users);

        $this->response->html(
            $this->template->layout('user_index', array(
                'projects' => $this->project->getList(),
                'users' => $users,
                'nb_users' => $nb_users,
                'menu' => 'users',
                'title' => t('Users').' ('.$nb_users.')'
        )));
    }

    // Display a form to create a new user
    public function create()
    {
        $this->response->html($this->template->layout('user_new', array(
            'projects' => $this->project->getList(),
            'errors' => array(),
            'values' => array(),
            'menu' => 'users',
            'title' => t('New user')
        )));
    }

    // Validate and save a new user
    public function save()
    {
        $values = $this->request->getValues();
        list($valid, $errors) = $this->user->validateCreation($values);

        if ($valid) {

            if ($this->user->create($values)) {
                $this->session->flash(t('User created successfully.'));
                $this->response->redirect('?controller=user');
            }
            else {
                $this->session->flashError(t('Unable to create your user.'));
            }
        }

        $this->response->html($this->template->layout('user_new', array(
            'projects' => $this->project->getList(),
            'errors' => $errors,
            'values' => $values,
            'menu' => 'users',
            'title' => t('New user')
        )));
    }

    // Display a form to edit a user
    public function edit()
    {
        $user = $this->user->getById($this->request->getIntegerParam('user_id'));

        if (! $user) $this->notfound();

        if (! $_SESSION['user']['is_admin'] && $_SESSION['user']['id'] != $user['id']) {
            $this->forbidden();
        }

        unset($user['password']);

        $this->response->html($this->template->layout('user_edit', array(
            'projects' => $this->project->filterListByAccess($this->project->getList(), $user['id']),
            'errors' => array(),
            'values' => $user,
            'menu' => 'users',
            'title' => t('Edit user')
        )));
    }

    // Validate and update a user
    public function update()
    {
        $values = $this->request->getValues();

        if ($_SESSION['user']['is_admin'] == 1) {
            $values += array('is_admin' => 0);
        }
        else {

            if ($_SESSION['user']['id'] != $values['id']) {
                $this->forbidden();
            }

            if (isset($values['is_admin'])) {
                unset($values['is_admin']); // Regular users can't be admin
            }
        }

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

        if ($valid) {

            if ($this->user->update($values)) {
                $this->session->flash(t('User updated successfully.'));
                $this->response->redirect('?controller=user');
            }
            else {
                $this->session->flashError(t('Unable to update your user.'));
            }
        }

        $this->response->html($this->template->layout('user_edit', array(
            'projects' => $this->project->filterListByAccess($this->project->getList(), $values['id']),
            'errors' => $errors,
            'values' => $values,
            'menu' => 'users',
            'title' => t('Edit user')
        )));
    }

    // Confirmation dialog before to remove a user
    public function confirm()
    {
        $user = $this->user->getById($this->request->getIntegerParam('user_id'));

        if (! $user) $this->notfound();

        $this->response->html($this->template->layout('user_remove', array(
            'user' => $user,
            'menu' => 'users',
            'title' => t('Remove user')
        )));
    }

    // Remove a user
    public function remove()
    {
        $user_id = $this->request->getIntegerParam('user_id');

        if ($user_id && $this->user->remove($user_id)) {
            $this->session->flash(t('User removed successfully.'));
        } else {
            $this->session->flashError(t('Unable to remove this user.'));
        }

        $this->response->redirect('?controller=user');
    }
}