-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcoCheckShell.php
More file actions
254 lines (229 loc) · 9.1 KB
/
Copy pathAcoCheckShell.php
File metadata and controls
254 lines (229 loc) · 9.1 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
<?php
App::uses('AppShell', 'Console/Command');
App::uses('Controller', 'Controller');
App::uses('ComponentCollection', 'Controller');
App::uses('AclComponent', 'Controller/Component');
/**
* Aco checker shell extension for CakePHP
* This extension will scan the entire app and display what ACO's are missing in ACL
*
* @author Abhishek Gupta <abhi.gupta200297@gmail.com>
* @version v1
*/
class AcoCheckShell extends AppShell
{
private $_acl;
/*The $_methodsBlacklist array is a list of controller actions you want to exclude from the
scan. It should be in the form
array(
'controller' => array('methodName1','methodName2'),
'controller1' => array('methodName1','methodName2')
)
*/
private $_methodsBlacklist = array();
private $_listOfControllers = array();
// Any controllers you want to put in Blacklist should be added here.
private $_controllersBlacklist = array('App');
private $_suggestedCommands = array();
private $_baseMethodNames = array();
/**
Shell startup method
*/
public function startup()
{
$collection = new ComponentCollection();
$this->_acl = new AclComponent($collection);
$controller = new Controller();
$this->_acl->startup($controller);
$this->_listOfControllers = $this->_getControllerNamesWithBlacklistRemoved();
$this->_baseMethodNames = $this->_getMethodNamesInClass('App');
$this->_printWelcomeMessage();
}
/**
* Main logic of the application
*/
public function main()
{
$aco = $this->_acl->Aco;
$rootControllerName = $this->_getUserInput("Enter root controller Name", 'controllers');
$root = $aco->node($rootControllerName);
if (!$root) {
echo "The root aco <$rootControllerName> does not exist. Please create it first.\n";
$this->_addToSuggestedCommands("cake acl create aco root $rootControllerName");
$this->_printSuggestedCommands();
exit();
}
foreach ($this->_listOfControllers as $controllerName) {
$methodList = $this->_getCleanedUpMethodsFromController($controllerName);
$controllerAcoFound = $aco->node('controllers/' . $controllerName);
if (!$controllerAcoFound) {
$this->out(__d('cake_console', "Could not find ACO for <<error>" . $controllerName . "</error>> controller and all its methods in the ACL."));
$this->_addToSuggestedCommands("cake acl create aco " . $rootControllerName . " " . $controllerName);
foreach ($methodList as $methodName) {
$this->_addToSuggestedCommands("cake acl create aco " . $rootControllerName . "/" . $controllerName . " " . $methodName);
}
} else {
$this->out(__d('cake_console', "Found ACO for <<success>" . $controllerName . "</success>> controller in the ACL."));
foreach ($methodList as $methodName) {
$methodAcoFound = $aco->node('controllers/' . $controllerName . '/' . $methodName);
if (!$methodAcoFound) {
$this->out(__d('cake_console', "\tACO for <<error>" . $methodName . "</error>> not found."));
$this->_addToSuggestedCommands("cake acl create aco " . $rootControllerName . "/" . $controllerName . " " . $methodName);
}
}
}
}
$this->_exitIfAcosAreUpToDate();
$wantToSeeSuggestions = $this->_getUserInput("Do you want to see command suggestions. Anything else to cancel", "yes");
if ($wantToSeeSuggestions == strtolower("yes")) {
$this->_printSuggestedCommands();
$wantToRunSuggestions = $this->_getUserInput("Do you want to run these suggestions automatically?. Anything else to cancel", "no");
if ($wantToRunSuggestions == strtolower("yes")) {
$this->_executeSuggestedCommands();
}
}
}
/**
* Check if suggested commands array is empty.
* If it is, the acos are up to date. We exit here.
*/
private function _exitIfAcosAreUpToDate()
{
if (empty($this->_suggestedCommands)) {
echo "\n";
$this->out(__d('cake_console', '<success>Your aco\'s seem up to date.</success>'));
exit();
}
}
/**
* We prepend proper path to cake app dir here and run all commands in the array.
*/
private function _executeSuggestedCommands()
{
foreach ($this->_suggestedCommands as $command) {
$commandToRun = APP . 'Console/' . $command;
echo "Executing: " . $commandToRun;
exec($commandToRun);
}
echo "\n\n";
$this->out(__d('cake_console', '<success>All commands executed...</success>'));
}
/**
* Gets all methods from a controller class.
* After that, removes any private methods starting with underscore,
* removes all methods which were inherited from the base controller class AppController
* Finally, removes any methods which were manually set by us in the blacklist array
* Returns the remaining method names after removing all these methods from the array
*
* @param $controllerName
* @return array
*/
private function _getCleanedUpMethodsFromController($controllerName)
{
$methods = $this->_getMethodNamesInClass($controllerName);
foreach ($methods as $index => $method) {
if (strpos($method, '_', 0) === 0) {
unset($methods[$index]);
continue;
}
if (in_array($method, $this->_baseMethodNames)) {
unset($methods[$index]);
continue;
}
// Match methods from Blacklist and clean them up from methods array
if (array_key_exists($controllerName, $this->_methodsBlacklist)) {
if (in_array($method, $this->_methodsBlacklist[$controllerName])) {
unset($methods[$index]);
continue;
}
}
}
return $methods;
}
/**
* Gets user input from console
*
* @param $message
* @param null $defaultValue
* @return null|string
*/
private function _getUserInput($message, $defaultValue = null)
{
if ($defaultValue != null) {
fwrite(STDOUT, "$message " . "[" . $defaultValue . "]: ");
} else {
fwrite(STDOUT, "$message: ");
}
$varIn = trim(fgets(STDIN));
if (!$varIn && $defaultValue != null) {
return $defaultValue;
}
return $varIn;
}
/**
* Takes in a command as an an input and adds it to the suggested commands array.
* This array will be used at later point to display results & execute the commands
* @param $command
*/
private function _addToSuggestedCommands($command)
{
$this->_suggestedCommands[] = $command;
}
/**
* Prints the suggested commands from _suggestedCommands array
*/
private function _printSuggestedCommands()
{
echo "\n";
if (APP != (exec('pwd') . '/')) {
$this->out(__d('cake_console', '<info>Please go to your app directory. Suggested commands are: </info>'));
} else {
$this->out(__d('cake_console', '<info>Suggested commands are: </info>'));
}
$this->hr();
echo "\n";
foreach ($this->_suggestedCommands as $command) {
echo "Console/" . $command . "\n";
}
echo "\n";
}
/**
* This method gets a list og all the controllers in the app.
* After getting a list, it removes all blacklisted controllers set by us in the beginning
* @return array
*/
private function _getControllerNamesWithBlacklistRemoved()
{
$controllerList = App::objects('Controller');
foreach ($controllerList as $index => $controllerNameToClean) {
$controllerList[$index] = preg_replace('/Controller$/', '', $controllerNameToClean);
}
$controllerList = array_diff($controllerList, $this->_controllersBlacklist);
return $controllerList;
}
/**
* This method gets the name of all class methods from a class.
* @param null $controllerName
* @return array
*/
function _getMethodNamesInClass($controllerName = null)
{
App::import('Controller', $controllerName);
$controllerClass = $controllerName . 'Controller';
$methods = get_class_methods($controllerClass);
return $methods;
}
/**
* Print welcome message
*/
private function _printWelcomeMessage()
{
$this->hr();
$this->out(__d('cake_console', '<info>CakePHP Aco checker extension</info>'));
$this->out(__d('cake_console', '<info>Developed By: Abhishek Gupta (1 Jan 2013)</info>'));
$this->out(__d('cake_console', '<info>Email: abhi.gupta200297@gmail.com</info>'));
$this->out(__d('cake_console', '<info>http://www.codingthoughts.com</info>'));
$this->hr();
echo "\n";
}
}