forked from henrystivens/backend
/
usuario_controller.php
230 lines (202 loc) · 8.15 KB
/
usuario_controller.php
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Backend - KumbiaPHP Backend
* PHP version 5
* LICENSE
*
* 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
* ERCHANTABILITY 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 <http://www.gnu.org/licenses/>.
*
* Controlador para solicitar cambio de clave y cambiar la clave del usuario.
*
* @license http://www.gnu.org/licenses/agpl.txt GNU AFFERO GENERAL PUBLIC LICENSE version 3.
* @author Henry Stivens Adarme Muñoz <henry.stivens@gmail.com>
*/
Load::models('seguridad/usuario');
class UsuarioController extends AppController {
public function before_filter() {
}
public function ver($nick) {
$usuario = new Usuario();
if(!$this->usuario = $usuario->findByNick($nick)){
Flash::warning('Este usuario no existe.');
Router::redirect('/');
}
$this->title = 'Perfil de ' . $this->usuario->nombre;
}
public function recordar_clave() {
$this->title = 'Recordar clave';
if (Input::hasPost('email_or_username')) {
try {
$email_or_username = Input::post('email_or_username');
$usuario = new Usuario();
if ($usuario->resetClaveByEmailOrUsername($email_or_username)) {
Flash::success('Se ha enviado un correo para confirmar el cambio de clave.');
Input::delete();
} else {
Flash::error('Oops ha ocurrido un error.');
Input::delete();
}
} catch (KumbiaException $kex) {
Input::delete();
Flash::warning("Lo sentimos ha ocurrido un error:");
Flash::error($kex->getMessage());
}
}
}
public function reportar_activacion() {
$this->title = 'Reportar activación';
if (Input::hasPost('email_or_username')) {
try {
$email_or_username = Input::post('email_or_username');
$usuario = new Usuario();
if ($usuario->reenviarActivacion($email_or_username)) {
Flash::success('En un momento recibira un correo de confirmación para activar su cuenta.');
return Router::redirect("usuario/ver/$usuario->nick/");
} else {
Flash::error('Oops ha ocurrido un error.');
Input::delete();
}
} catch (KumbiaException $kex) {
Input::delete();
Flash::warning("Lo sentimos ha ocurrido un error:");
Flash::error($kex->getMessage());
}
}
}
public function cambiar_clave($email, $reset_clave) {
$this->title = 'Cambiar clave del usuario';
$usuario = new Usuario();
$usuario = $usuario->findByEmail($email);
$this->id = $usuario->id;
if ($usuario->reset == $reset_clave) {
if (Input::hasPost('usuario')) {
try {
$data = Input::post('usuario');
if (Load::model('usuario')->cambiar_clave($data['id'], $data['clave'], $data['clave2'])) {
Flash::success('Cambio de clave realizado exitosamente.');
return Router::redirect('/');
} else {
Input::delete();
}
} catch (KumbiaException $kex) {
Input::delete();
Flash::warning("Lo sentimos ha ocurrido un error:");
Flash::error($kex->getMessage());
}
}
} else {
Flash::error('La clave para reseteo es incorrecta o ya fue usado.');
return Router::redirect('usuario/recordar_clave/');
}
}
public function cambiar_clave2($nick) {
View::select('cambiar_clave');
$this->title = 'Cambiar clave del usuario';
$usuario = new Usuario();
$usuario = $usuario->findByNick($nick);
if($usuario->id != Auth::get('id')){
Flash::warning('Uste no puede editar la información de este usuario');
return Router::redirect("usuario/ver/$usuario->nick/");
}
$this->id = $usuario->id;
if (Input::hasPost('usuario')) {
try {
$data = Input::post('usuario');
if (Load::model('usuario')->cambiar_clave($data['id'], $data['clave'], $data['clave2'])) {
Flash::success('Cambio de clave realizado exitosamente.');
return Router::redirect('/');
} else {
Input::delete();
}
} catch (KumbiaException $kex) {
Input::delete();
Flash::warning("Lo sentimos ha ocurrido un error:");
Flash::error($kex->getMessage());
}
}
}
/**
* Crear un nuevo usuario.
* @return View
*/
public function registro() {
if(Auth::is_valid()){
return Router::redirect('/');
}
$this->title = 'Formulario de registro';
if (Input::hasPost('usuario')) {
$obj = Load::model('usuario');
if ($obj->save(Input::post('usuario'))) {
Flash::success('En un momento recibira un correo de confirmación para activar su cuenta.');
return Router::redirect("usuario/ver/$obj->nick/");
}else{
Flash::error('Falló operación');
$this->usuario = $obj;
$this->usuario->clave = '';
return false;
}
}
}
/**
* Edita un usuario.
* @return View
*/
public function editar($nick) {
$usuario = new Usuario();
$this->usuario = $usuario->findByNick($nick);
if($this->usuario->id != Auth::get('id')){
Flash::warning('Usted no puede editar la información de este usuario');
return Router::redirect("usuario/ver/$usuario->nick/");
}
$this->title = 'Editar perfil de ' . $this->usuario->nombre;
if (Input::hasPost('usuario')) {
$data = Input::post('usuario');
if ($usuario->editar($data['id'],$data['nombre'],$data['nick'],$data['web'],$data['bio'])) {
Flash::success('Información actualizada.');
return Router::redirect("usuario/ver/{$data['nick']}/");
}else{
Flash::error('Falló operación');
$this->usuario = $data;
return false;
}
}
}
public function activar($id, $reset_clave) {
$this->title = 'Activar cuenta';
$usuario = new Usuario();
$usuario = $usuario->find($id);
if ($usuario->reset == $reset_clave) {
$usuario->activo = 'si';
$usuario->update();
Flash::success('Ha activado su cuenta correctamente. Ahora puede iniciar sesion:');
return Router::redirect('usuario/login/');
} else {
Flash::error('La clave para activar la cuenta es incorrecta o ya fue usado.');
return Router::redirect("usuario/ver/$usuario->nick/");
}
}
public function login() {
$this->title = 'Iniciar sesión';
Load::lib('SdAuth');
if (!SdAuth::isLogged()) {
if(Input::hasPost('txt_login')){
Flash::warning(SdAuth::getError());
}
Input::delete('txt_password');
return FALSE;
}else{
return Router::redirect('/');
}
}
}