1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * To change this template, choose Tools | Templates |
---|
5 | * and open the template in the editor. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Description of users_controller |
---|
10 | * |
---|
11 | * @author fpvanagthoven |
---|
12 | */ |
---|
13 | class UsersController extends AppController { |
---|
14 | |
---|
15 | var $name = 'Users'; |
---|
16 | var $components = array('Auth'); // Not necessary if declared in your app controller |
---|
17 | /** |
---|
18 | * |
---|
19 | * @var SessionComponent |
---|
20 | */ |
---|
21 | var $Session; |
---|
22 | /** |
---|
23 | * |
---|
24 | * @var AuthComponent |
---|
25 | */ |
---|
26 | var $Auth; |
---|
27 | |
---|
28 | /** |
---|
29 | * The AuthComponent provides the needed functionality |
---|
30 | * for login, so you can leave this function blank. |
---|
31 | */ |
---|
32 | function beforeFilter() { |
---|
33 | parent::beforeFilter(); |
---|
34 | |
---|
35 | $this->Auth->allow('register'); |
---|
36 | } |
---|
37 | |
---|
38 | function login() { |
---|
39 | // Enable a flash message after a successful login |
---|
40 | // Check if user could be logged in and set flash message if so |
---|
41 | if ($this->Auth->login($this->data)) { |
---|
42 | $this->redirect(array('controller' => 'pages', 'action' => 'mainmenu')); |
---|
43 | } |
---|
44 | // If user is already logged in redirect |
---|
45 | else if ($this->Auth->user()) { |
---|
46 | $this->redirect(array('controller' => 'pages', 'action' => 'mainmenu')); |
---|
47 | } |
---|
48 | else // bad login |
---|
49 | { |
---|
50 | $this->redirect('/'); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | function logout() { |
---|
55 | $this->redirect($this->Auth->logout()); |
---|
56 | } |
---|
57 | |
---|
58 | function register() { |
---|
59 | if (!empty($this->data)) { |
---|
60 | $this->data['User']['password_confirmation'] = |
---|
61 | $this->Auth->password($this->data['User']['password_confirmation']); |
---|
62 | |
---|
63 | if ($this->data['User']['password'] == $this->data['User']['password_confirmation']) { |
---|
64 | $this->Session->setFlash('Registered ' . $this->data['User']['username'] . '!'); |
---|
65 | |
---|
66 | $this->User->create(); |
---|
67 | $this->User->save($this->data); |
---|
68 | } else { |
---|
69 | $this->Session->setFlash('Could not register. Try again.'); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | ?> |
---|