forked from CauanCabral/Comitiva
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_controller.php
More file actions
executable file
·317 lines (279 loc) · 7.08 KB
/
app_controller.php
File metadata and controls
executable file
·317 lines (279 loc) · 7.08 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* PHP versions 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Comitiva : Sistema de gerenciamento de eventos ( http://comitiva.phpms.org )
* Copyright 2010, Grupo de Desenvolvedores PHP de Mato Grosso do Sul - PHPMS ( http://phpms.org )
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @copyright Copyright 2010, Grupo de Desenvolvedores PHP de Mato Grosso do Sul - PHPMS ( http://phpms.org )
* @link http://comitiva.phpms.org Comitiva Project
* @package cake
* @subpackage cake.comitiva
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Base of all Application Controllers
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package cake
* @subpackage cake.comitiva
*/
class AppController extends Controller
{
/****************************
* Cake Controller atributes
****************************/
public $components = array('Auth', 'RequestHandler', 'Session');
public $helpers = array('Html', 'Form', 'Js', 'Session');
public $uses = array(); // ATENÇÃO! não carregue modelos no AppController. Em último caso utilize Controller::loadModel
public $paginate = array('limit' => 50);
/************************
* Aditional atributes
***********************/
/**
* TRUE if user has logged
*
* @var boolean
*/
public $userLogged = false;
/**
* Reference for current user
*
* @var User
*/
public $activeUser;
/*************************
* Cake Callbacks
************************/
/**
* Callback default
*
* @return void
*/
public function beforeFilter()
{
$this->__setupAuth();
if($this->userLogged === true)
{
$this->__buildMenu();
}
parent::beforeFilter();
}
/**
* Callback default
*
* @return void
*/
public function beforeRender()
{
$this->__setErrorLayout();
}
public function isAuthorized()
{
if($this->userLogged === TRUE && $this->__checkGroup($this->params['prefix']) )
{
return true;
}
return false;
}
/***************************
* Auxiliar methods
**************************/
private function __setupAuth()
{
//Configure AuthComponent
$this->Auth->authorize = 'controller';
if( !isset($this->params['prefix']) || !( in_array($this->params['prefix'], Configure::read('Routing.prefixes')) ) )
{
// all non-prefixed actions are allowed
$this->Auth->allow('*');
}
$this->Auth->autoRedirect = false;
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'participant' => false);
$this->Auth->logoutRedirect = '/';
$this->Auth->loginRedirect = '/estatica/logged';
// What to say when the login was incorrect.
$this->Auth->loginError = __('Falha no login. Por favor, verifique se o usuário e senha digitado estão corretos.', TRUE);
// What to say when unauthorized access has detected
$this->Auth->authError = __('Desculpe, você precisa estar autenticado para acessar esta página.', TRUE);
// tmp var to load logged user information
$this->activeUser = $this->Auth->user();
if($this->activeUser != null)
{
// set control flag
$this->userLogged = true;
// Define a static access to user information
App::import('Model', 'User');
User::store($this->activeUser);
// Define user information in view class
$this->set('activeUser', $this->activeUser);
if($this->here == '/')
{
$this->redirect('/estatica/logged');
}
}
else
{
// if not authenticated, alter display layout
$this->layout = 'login';
}
}
/**
* TODO make this a dynamic menu generate
*
* @return unknown_type
*/
private function __buildMenu()
{
//$groups = json_decode(User::get('groups'), true);
if($this->__checkGroup('admin'))
{
$menu = array(
__('Eventos', TRUE) => array(
'controller' => 'events',
'action' => 'index',
'admin' => true
),
__('Pagamentos', TRUE) => array(
'controller' => 'payments',
'action' => 'index',
'admin' => true
),
__('Usuários', TRUE) => array(
'controller' => 'users',
'action' => 'index',
'admin' => true
),
__('Mensagens', TRUE) => array(
'controller' => 'messages',
'action' => 'index',
'admin' => true
),
__('Propostas', TRUE) => array(
'controller' => 'proposals',
'action' => 'index',
'admin' => true
),
__('Minha conta',TRUE) => array(
'controller' => 'users',
'action' => 'profile',
'admin' => true
),
__('Sair', TRUE) => '/logout',
);
}
else if($this->__checkGroup('participant'))
{
$menu = array(
__('Eventos', TRUE) => array(
'controller' => 'events',
'action' => 'index',
'participant' => true
)
);
if($this->__checkGroup('speaker'))
{
$menu[__('Propostas', TRUE)] = array(
'controller' => 'proposals',
'action' => 'index',
'speaker' => true
);
}
else
{
$menu[__('Propostas', TRUE)] = array(
'controller' => 'proposals',
'action' => 'add',
'participant' => true
);
}
$menu[__('Minha conta', TRUE)] = array(
'controller' => 'users',
'action' => 'profile',
'participant' => true
);
$menu[__('Sair', TRUE)] = array(
'controller' => 'users',
'action' => 'logout',
'admin' => false,
'participant' => false,
'speaker' => false
);
}
else
{
$menu = array();
}
$this->set('menuItems', $menu);
}
/**
* Método auxiliar para verificar se um determinado usuário
* pertence a um determinado grupo.
*
* @param string $group
* @param array $user_groups optional
*
* @return bool TRUE caso pertença, FALSE caso contrário
*/
protected function __checkGroup($group, $user_groups = null)
{
if($user_groups == null)
{
$groups = json_decode(User::get('groups'), true);
}
else
{
$groups = $user_groups;
}
foreach($groups as $g)
{
$tmp = str_replace('"', '', $g);
if($tmp === $group)
return true;
}
return false;
}
/**
* Override Error Handling of CakePHP to use a proper layout file
*
* @return void
*/
private function __setErrorLayout()
{
if($this->name == 'CakeError')
{
//@TODO create the error layout
//$this->layout = 'error';
}
}
/**
* This method prepare default a ajax response
*/
protected function __prepareAjax($autoRender = false)
{
Configure::write('Cache.disable', true);
Configure::write('debug', 0);
$this->autoRender = $autoRender;
}
/**
* Try redirect to origin action
*
* @return void
*/
protected function __goBack()
{
// tenta redirecionar de volta para tela anterior, caso não consiga manda par action index
$this->redirect($this->referer(array('action'=>'index'), TRUE));
}
}
?>