-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_controller.php
More file actions
executable file
·118 lines (107 loc) · 3 KB
/
app_controller.php
File metadata and controls
executable file
·118 lines (107 loc) · 3 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
<?php
class AppController extends Controller {
var $components = array(
'Auth',
'Session',
'DebugKit.Toolbar'
);
var $helpers = array(
'UploadPack.Upload',
'Session',
'Form',
'Text',
'Time',
'ProCode'
);
function beforeFilter() {
$this->__configureAuth();
App::import('Model', 'User');
User::store($this->Auth->user());
}
function beforeRender() {
// Configure Layout
if ($this->_prefix()) {
$this->layout = 'admin';
}
// Load common layout variables
$this->loadModel('User');
$popUsers = $this->User->find('list', array('limit' => 10));
$reviewCount = $this->User->Review->find('count');
$this->set(compact('popUsers', 'reviewCount'));
}
/**
* Checks to see if the current user is the owner of the record and sets a boolean variable to the view
*
* @param $id int id of the current record to check ownership for
*/
function _owner($id, $relatedModel = null) {
if ($relatedModel) {
$check = $this->{$this->modelClass}->$relatedModel->field('user_id', array('id' => $id));
} elseif ($this->modelClass == 'User') {
$check = $id;
} else {
$check = $this->{$this->modelClass}->field($this->modelClass.'.user_id', array($this->modelClass.'.id' => $id));
}
if ($this->Auth->user('id') == $check) {
$this->set('owner', true);
return true;
} else {
$this->set('owner', false);
return false;
}
}
/**
* Checks to see if the current user is a subscriber and sends subscription info to the view
* Can be used in conjunction with the 'subscribe.ctp' element
*
* @param $id int id of the current record to check a subscription for
*/
function _subscriber($id) {
$results = $this->{$this->modelClass}->Subscriber->find('first', array(
'conditions' => array(
'foreign_model' => $this->modelClass,
'foreign_id' => $id,
'user_id' => $this->Auth->user('id')
),
'recursive' => -1,
));
$this->set('subscriber', $results);
return $results;
}
/**
* Checks to see what the current prefix in use is. Checks for 'admin' by
* default.
*
* @return boolean
* @access protected
**/
function _prefix($prefix = 'admin') {
if (isset($this->params['prefix']) && $this->params['prefix'] == $prefix) {
return true;
}
return false;
}
/**
* Configures the AuthComponent according to the application's settings
*
* @return void
* @access private
*/
function __configureAuth() {
$this->Auth->fields = array('username' => 'username', 'password' => 'password');
$this->Auth->loginAction = array('plugin' => null, 'admin' => false, 'controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = '/';
$this->Auth->loginRedirect = array('controller' => 'reviews', 'action' => 'mine');
if ($this->_prefix()) {
$this->Auth->deny();
if ($this->Auth->user('role') != 'Admin') {
$this->Session->setFlash('You must be an Admin to access this area');
$this->redirect($this->Auth->loginAction);
}
} else {
$this->Auth->allow();
$this->Auth->deny(array('add', 'edit', 'delete'));
}
}
}
?>