Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class UsersController extends AppController {
- var $name = 'Users';
- var $components = array('Auth','Email'); // Not necessary if declared in your app controller
- function beforeFilter() {
- parent::beforeFilter();
- //$this->Auth->allowedActions = array('index', 'view');
- //$this->Auth->allowedActions = array('*');
- $this->Auth->allow('login','register', 'message','success','view','passwordReset','setPermissions');
- }
- function index() {
- $this->User->recursive = 0;
- $this->set('users', $this->paginate());
- }
- function view($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid user', true),'default');
- $this->redirect(array('action' => 'index'));
- }
- $this->set('user', $this->User->read(null, $id));
- }
- function edit() {
- }
- function login() {
- }
- function message(){
- }
- function success(){}
- function logout() {
- $this->Session->destroy();
- $this->redirect($this->Auth->logout());
- }
- function check_mail($adres) {
- return ereg("[A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+",$adres);
- }
- function register() {
- if (!empty($this->data)) {
- //$password = $this->Auth->password($this->data["User"]["password"]);
- //$this->data['User']['password'] = $password;
- $this->data['User']['group_id'] = 5;
- $this->User->save($this->data);
- $this->Session->setFlash("Email send, please check your email for your CakeOTP",'default');
- echo $this->data['User']['password'];
- //$this->redirect("/");
- }
- }
- function passwordReset() {
- if (!empty($this->data)) {
- $user = $this->User->FindByUsername($this->data["User"]["username"]);
- if($user){
- $newPass = $this->generatePassword();
- $randomPass = $this->Auth->password($newPass);
- $user["User"]["password"] = $randomPass;
- $user["User"]["modified"] = date('Y-m-d G:i:s');
- $this->User->save($user);
- //$this->User->saveField('password', $this->Auth->password($randomPass));
- // send mail
- $this->Email->from = "jerre@fom.be";
- $this->Email->to = $this->data["User"]["username"];
- $this->Email->subject = "FoM Website Password reset";
- $this->Email->sendAs = 'html';
- $body = "Hi " . $user["User"]["nickname"] . ", <br><br>We received a password reset request for your account, your new password is:<br>";
- $body .= $newPass;
- $this->Email->send($body);
- $this->Session->setFlash("An E-mail with a new password has been sent!",'default');
- $this->redirect("/");
- } else {
- // error: no user found for specified email
- $this->Session->setFlash("There is nu user registrated with the specified E-mail.",'default');
- }
- }
- }
- function setPermissions() {
- $group =& $this->User->Group;
- //Allow admins to everything
- $group->id = 3;
- $this->Acl->allow($group, 'controllers');
- //allow managers to posts and widgets
- //$group->id = 4;
- //$this->Acl->deny($group, 'controllers');
- //$this->Acl->allow($group, 'controllers/Users');
- //$this->Acl->allow($group, 'controllers/Groups');
- //allow users to only add and edit on posts and widgets
- $group->id = 5;
- $this->Acl->deny('Users', 'controllers');
- $this->Acl->allow('Users', 'controllers/Orders/cart');
- //$this->Acl->allow('Users', 'controllers/Users/assignticket');
- //$this->Acl->allow('Users', 'controllers/Groups/view');
- /*$this->Acl->allow('Users','controllers/Users/tickets');
- $this->Acl->allow('Users','controllers/Orders/index');
- $this->Acl->allow('Users','controllers/Orders/view');
- $this->Acl->allow('Users','controllers/Orders/place');
- $this->Acl->allow('Users','controllers/Orders/overview');
- $this->Acl->allow('Users','controllers/Orders/confirm');
- $this->Acl->allow('Users','controllers/Orders/done');
- $this->Acl->allow('Users','controllers/Orders/success');
- $this->Acl->allow('Users','controllers/Orders/cancel');
- $this->Acl->allow('Users','controllers/Orders/addItem');*/
- //$this->Acl->allow('Users','controllers/Orders/adjust');
- //$this->Acl->allow('Users','controllers/Users/assignticket2');
- //$this->Acl->allow('Users','controllers/Users/oldLogin');
- }
- /// automate ACL lookup
- function build_acl() {
- if (!Configure::read('debug')) {
- return $this->_stop();
- }
- $log = array();
- $aco =& $this->Acl->Aco;
- $root = $aco->node('controllers');
- if (!$root) {
- $aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers'));
- $root = $aco->save();
- $root['Aco']['id'] = $aco->id;
- $log[] = 'Created Aco node for controllers';
- } else {
- $root = $root[0];
- }
- App::import('Core', 'File');
- $Controllers = Configure::listObjects('controller');
- $appIndex = array_search('App', $Controllers);
- if ($appIndex !== false ) {
- unset($Controllers[$appIndex]);
- }
- $baseMethods = get_class_methods('Controller');
- $baseMethods[] = 'buildAcl';
- $Plugins = $this->_getPluginControllerNames();
- $Controllers = array_merge($Controllers, $Plugins);
- // look at each controller in app/controllers
- foreach ($Controllers as $ctrlName) {
- $methods = $this->_getClassMethods($this->_getPluginControllerPath($ctrlName));
- // Do all Plugins First
- if ($this->_isPlugin($ctrlName)){
- $pluginNode = $aco->node('controllers/'.$this->_getPluginName($ctrlName));
- if (!$pluginNode) {
- $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $this->_getPluginName($ctrlName)));
- $pluginNode = $aco->save();
- $pluginNode['Aco']['id'] = $aco->id;
- $log[] = 'Created Aco node for ' . $this->_getPluginName($ctrlName) . ' Plugin';
- }
- }
- // find / make controller node
- $controllerNode = $aco->node('controllers/'.$ctrlName);
- if (!$controllerNode) {
- if ($this->_isPlugin($ctrlName)){
- $pluginNode = $aco->node('controllers/' . $this->_getPluginName($ctrlName));
- $aco->create(array('parent_id' => $pluginNode['0']['Aco']['id'], 'model' => null, 'alias' => $this->_getPluginControllerName($ctrlName)));
- $controllerNode = $aco->save();
- $controllerNode['Aco']['id'] = $aco->id;
- $log[] = 'Created Aco node for ' . $this->_getPluginControllerName($ctrlName) . ' ' . $this->_getPluginName($ctrlName) . ' Plugin Controller';
- } else {
- $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $ctrlName));
- $controllerNode = $aco->save();
- $controllerNode['Aco']['id'] = $aco->id;
- $log[] = 'Created Aco node for ' . $ctrlName;
- }
- } else {
- $controllerNode = $controllerNode[0];
- }
- //clean the methods. to remove those in Controller and private actions.
- foreach ($methods as $k => $method) {
- if (strpos($method, '_', 0) === 0) {
- unset($methods[$k]);
- continue;
- }
- if (in_array($method, $baseMethods)) {
- unset($methods[$k]);
- continue;
- }
- $methodNode = $aco->node('controllers/'.$ctrlName.'/'.$method);
- if (!$methodNode) {
- $aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method));
- $methodNode = $aco->save();
- $log[] = 'Created Aco node for '. $method;
- }
- }
- }
- if(count($log)>0) {
- debug($log);
- }
- }
- function _getClassMethods($ctrlName = null) {
- App::import('Controller', $ctrlName);
- if (strlen(strstr($ctrlName, '.')) > 0) {
- // plugin's controller
- $num = strpos($ctrlName, '.');
- $ctrlName = substr($ctrlName, $num+1);
- }
- $ctrlclass = $ctrlName . 'Controller';
- $methods = get_class_methods($ctrlclass);
- // Add scaffold defaults if scaffolds are being used
- $properties = get_class_vars($ctrlclass);
- if (array_key_exists('scaffold',$properties)) {
- if($properties['scaffold'] == 'admin') {
- $methods = array_merge($methods, array('admin_add', 'admin_edit', 'admin_index', 'admin_view', 'admin_delete'));
- } else {
- $methods = array_merge($methods, array('add', 'edit', 'index', 'view', 'delete'));
- }
- }
- return $methods;
- }
- function _isPlugin($ctrlName = null) {
- $arr = String::tokenize($ctrlName, '/');
- if (count($arr) > 1) {
- return true;
- } else {
- return false;
- }
- }
- function _getPluginControllerPath($ctrlName = null) {
- $arr = String::tokenize($ctrlName, '/');
- if (count($arr) == 2) {
- return $arr[0] . '.' . $arr[1];
- } else {
- return $arr[0];
- }
- }
- function _getPluginName($ctrlName = null) {
- $arr = String::tokenize($ctrlName, '/');
- if (count($arr) == 2) {
- return $arr[0];
- } else {
- return false;
- }
- }
- function _getPluginControllerName($ctrlName = null) {
- $arr = String::tokenize($ctrlName, '/');
- if (count($arr) == 2) {
- return $arr[1];
- } else {
- return false;
- }
- }
- /**
- * Get the names of the plugin controllers ...
- *
- * This function will get an array of the plugin controller names, and
- * also makes sure the controllers are available for us to get the
- * method names by doing an App::import for each plugin controller.
- *
- * @return array of plugin names.
- *
- */
- function _getPluginControllerNames() {
- App::import('Core', 'File', 'Folder');
- $paths = Configure::getInstance();
- $folder =& new Folder();
- $folder->cd(APP . 'plugins');
- // Get the list of plugins
- $Plugins = $folder->read();
- $Plugins = $Plugins[0];
- $arr = array();
- // Loop through the plugins
- foreach($Plugins as $pluginName) {
- // Change directory to the plugin
- $didCD = $folder->cd(APP . 'plugins'. DS . $pluginName . DS . 'controllers');
- // Get a list of the files that have a file name that ends
- // with controller.php
- $files = $folder->findRecursive('.*_controller\.php');
- // Loop through the controllers we found in the plugins directory
- foreach($files as $fileName) {
- // Get the base file name
- $file = basename($fileName);
- // Get the controller name
- $file = Inflector::camelize(substr($file, 0, strlen($file)-strlen('_controller.php')));
- if (!preg_match('/^'. Inflector::humanize($pluginName). 'App/', $file)) {
- if (!App::import('Controller', $pluginName.'.'.$file)) {
- debug('Error importing '.$file.' for plugin '.$pluginName);
- } else {
- /// Now prepend the Plugin name ...
- // This is required to allow us to fetch the method names.
- $arr[] = Inflector::humanize($pluginName) . "/" . $file;
- }
- }
- }
- }
- return $arr;
- }
- function admin_edit($id = null) {
- if (!$id && empty($this->data)) {
- $this->Session->setFlash(__('Invalid user', true),'default');
- $this->redirect(array('action' => 'index'));
- }
- if (!empty($this->data)) {
- $error = 0;
- if($this->data['User']['password1'] != "") {
- if($this->data['User']['password1'] == $this->data['User']['password2']) {
- $this->data['User']['password'] = $this->Auth->password($this->data['User']['password1']);
- } else {
- $this->Session->setFlash(__('The passwords provided do no match', true),'default');
- $error = 1;
- }
- }
- if ($error == 0 && $this->User->save($this->data)) {
- $this->Session->setFlash(__('The user has been saved', true),'default');
- $this->redirect(array('action' => 'index'));
- } else if($error == 0) {
- $this->Session->setFlash(__('The user could not be saved. Please, try again.', true),'default');
- }
- }
- if (empty($this->data)) {
- $this->data = $this->User->read(null, $id);
- $this->data['User']['password1'] = "";
- $this->data['User']['password2'] = "";
- }
- $groups = $this->User->Group->find('list');
- $this->set(compact('groups'));
- }
- function admin_index($id = null) {
- $users = null;
- if(!empty($this->data)) {
- // search function
- $keywords = $this->data['User']['search'];
- $users = $this->User->query("SELECT *
- FROM users AS User
- WHERE (User.username LIKE '%" . $keywords . "%'
- OR User.firstname LIKE '%" . $keywords . "%'
- OR User.lastname LIKE '%" . $keywords . "%'
- OR User.nickname LIKE '%" . $keywords . "%')");
- }
- $this->set('users',$users);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement