Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.61 KB | None | 0 0
  1. <?php
  2. class UsersController extends AppController {
  3.  
  4.     var $name = 'Users';
  5.     var $components = array('Auth','Email'); // Not necessary if declared in your app controller
  6.    
  7.     function beforeFilter() {
  8.         parent::beforeFilter();
  9.         //$this->Auth->allowedActions = array('index', 'view');
  10.         //$this->Auth->allowedActions = array('*');
  11.         $this->Auth->allow('login','register', 'message','success','view','passwordReset','setPermissions');
  12.     }
  13.    
  14.     function index() {
  15.         $this->User->recursive = 0;
  16.         $this->set('users', $this->paginate());
  17.     }
  18.    
  19.     function view($id = null) {
  20.         if (!$id) {
  21.             $this->Session->setFlash(__('Invalid user', true),'default');
  22.             $this->redirect(array('action' => 'index'));
  23.         }
  24.         $this->set('user', $this->User->read(null, $id));
  25.     }
  26.    
  27.     function edit() {
  28.     }
  29.    
  30.     function login() {    
  31.        
  32.     }
  33.    
  34.     function message(){
  35.    
  36.     }
  37.                        
  38.     function success(){}        
  39.    
  40.     function logout() {
  41.         $this->Session->destroy();
  42.         $this->redirect($this->Auth->logout());
  43.     }
  44.    
  45.     function check_mail($adres) {  
  46.         return ereg("[A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+",$adres);  
  47.     }  
  48.    
  49.     function register() {
  50.         if (!empty($this->data)) {                                        
  51.                     //$password = $this->Auth->password($this->data["User"]["password"]);
  52.                     //$this->data['User']['password'] = $password;    
  53.                     $this->data['User']['group_id'] = 5;        
  54.                     $this->User->save($this->data);
  55.                     $this->Session->setFlash("Email send, please check your email for your CakeOTP",'default');
  56.                     echo $this->data['User']['password'];
  57.                     //$this->redirect("/");
  58.                 }
  59.     }
  60.    
  61.    
  62.     function passwordReset() {
  63.         if (!empty($this->data)) {
  64.             $user = $this->User->FindByUsername($this->data["User"]["username"]);
  65.             if($user){
  66.                 $newPass = $this->generatePassword();
  67.                 $randomPass = $this->Auth->password($newPass);
  68.                 $user["User"]["password"] = $randomPass;
  69.                 $user["User"]["modified"] = date('Y-m-d G:i:s');
  70.                 $this->User->save($user);
  71.                 //$this->User->saveField('password',   $this->Auth->password($randomPass));
  72.    
  73.                 // send mail
  74.                 $this->Email->from    = "jerre@fom.be";
  75.                 $this->Email->to      = $this->data["User"]["username"];
  76.    
  77.                 $this->Email->subject = "FoM Website Password reset";
  78.                 $this->Email->sendAs = 'html';
  79.    
  80.                 $body = "Hi " . $user["User"]["nickname"] . ", <br><br>We received a password reset request for your account, your new password is:<br>";
  81.                 $body .= $newPass;
  82.    
  83.                 $this->Email->send($body);
  84.                 $this->Session->setFlash("An E-mail with a new password has been sent!",'default');
  85.                 $this->redirect("/");
  86.             } else {
  87.                 // error: no user found for specified email
  88.                 $this->Session->setFlash("There is nu user registrated with the specified E-mail.",'default');
  89.             }
  90.         }
  91.     }          
  92.    
  93.     function setPermissions() {
  94.         $group =& $this->User->Group;
  95.         //Allow admins to everything
  96.         $group->id = 3;    
  97.         $this->Acl->allow($group, 'controllers');
  98.    
  99.         //allow managers to posts and widgets
  100.         //$group->id = 4;
  101.         //$this->Acl->deny($group, 'controllers');
  102.         //$this->Acl->allow($group, 'controllers/Users');
  103.         //$this->Acl->allow($group, 'controllers/Groups');
  104.    
  105.         //allow users to only add and edit on posts and widgets
  106.         $group->id = 5;
  107.         $this->Acl->deny('Users', 'controllers');        
  108.         $this->Acl->allow('Users', 'controllers/Orders/cart');
  109.         //$this->Acl->allow('Users', 'controllers/Users/assignticket');
  110.         //$this->Acl->allow('Users', 'controllers/Groups/view');
  111.        
  112.         /*$this->Acl->allow('Users','controllers/Users/tickets');
  113.         $this->Acl->allow('Users','controllers/Orders/index');
  114.         $this->Acl->allow('Users','controllers/Orders/view');
  115.         $this->Acl->allow('Users','controllers/Orders/place');
  116.         $this->Acl->allow('Users','controllers/Orders/overview');
  117.         $this->Acl->allow('Users','controllers/Orders/confirm');
  118.         $this->Acl->allow('Users','controllers/Orders/done');
  119.         $this->Acl->allow('Users','controllers/Orders/success');
  120.         $this->Acl->allow('Users','controllers/Orders/cancel');
  121.         $this->Acl->allow('Users','controllers/Orders/addItem');*/
  122.         //$this->Acl->allow('Users','controllers/Orders/adjust');
  123.         //$this->Acl->allow('Users','controllers/Users/assignticket2');
  124.         //$this->Acl->allow('Users','controllers/Users/oldLogin');
  125.     }
  126.    
  127.    
  128.    
  129.    
  130.     /// automate ACL lookup
  131.    
  132.     function build_acl() {
  133.         if (!Configure::read('debug')) {
  134.             return $this->_stop();
  135.         }
  136.         $log = array();
  137.    
  138.         $aco =& $this->Acl->Aco;
  139.         $root = $aco->node('controllers');
  140.         if (!$root) {
  141.             $aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers'));
  142.             $root = $aco->save();
  143.             $root['Aco']['id'] = $aco->id;
  144.             $log[] = 'Created Aco node for controllers';
  145.         } else {
  146.             $root = $root[0];
  147.         }  
  148.    
  149.         App::import('Core', 'File');
  150.         $Controllers = Configure::listObjects('controller');
  151.         $appIndex = array_search('App', $Controllers);
  152.         if ($appIndex !== false ) {
  153.             unset($Controllers[$appIndex]);
  154.         }
  155.         $baseMethods = get_class_methods('Controller');
  156.         $baseMethods[] = 'buildAcl';
  157.    
  158.         $Plugins = $this->_getPluginControllerNames();
  159.         $Controllers = array_merge($Controllers, $Plugins);
  160.    
  161.         // look at each controller in app/controllers
  162.         foreach ($Controllers as $ctrlName) {
  163.             $methods = $this->_getClassMethods($this->_getPluginControllerPath($ctrlName));
  164.    
  165.             // Do all Plugins First
  166.             if ($this->_isPlugin($ctrlName)){
  167.                 $pluginNode = $aco->node('controllers/'.$this->_getPluginName($ctrlName));
  168.                 if (!$pluginNode) {
  169.                     $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $this->_getPluginName($ctrlName)));
  170.                     $pluginNode = $aco->save();
  171.                     $pluginNode['Aco']['id'] = $aco->id;
  172.                     $log[] = 'Created Aco node for ' . $this->_getPluginName($ctrlName) . ' Plugin';
  173.                 }
  174.             }
  175.             // find / make controller node
  176.             $controllerNode = $aco->node('controllers/'.$ctrlName);
  177.             if (!$controllerNode) {
  178.                 if ($this->_isPlugin($ctrlName)){
  179.                     $pluginNode = $aco->node('controllers/' . $this->_getPluginName($ctrlName));
  180.                     $aco->create(array('parent_id' => $pluginNode['0']['Aco']['id'], 'model' => null, 'alias' => $this->_getPluginControllerName($ctrlName)));
  181.                     $controllerNode = $aco->save();
  182.                     $controllerNode['Aco']['id'] = $aco->id;
  183.                     $log[] = 'Created Aco node for ' . $this->_getPluginControllerName($ctrlName) . ' ' . $this->_getPluginName($ctrlName) . ' Plugin Controller';
  184.                 } else {
  185.                     $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $ctrlName));
  186.                     $controllerNode = $aco->save();
  187.                     $controllerNode['Aco']['id'] = $aco->id;
  188.                     $log[] = 'Created Aco node for ' . $ctrlName;
  189.                 }
  190.             } else {
  191.                 $controllerNode = $controllerNode[0];
  192.             }
  193.    
  194.             //clean the methods. to remove those in Controller and private actions.
  195.             foreach ($methods as $k => $method) {
  196.                 if (strpos($method, '_', 0) === 0) {
  197.                     unset($methods[$k]);
  198.                     continue;
  199.                 }
  200.                 if (in_array($method, $baseMethods)) {
  201.    
  202.                     unset($methods[$k]);
  203.                     continue;
  204.                 }
  205.                 $methodNode = $aco->node('controllers/'.$ctrlName.'/'.$method);
  206.                 if (!$methodNode) {
  207.                     $aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method));
  208.                     $methodNode = $aco->save();
  209.                     $log[] = 'Created Aco node for '. $method;
  210.                 }
  211.             }
  212.         }
  213.         if(count($log)>0) {
  214.             debug($log);
  215.         }
  216.     }
  217.    
  218.     function _getClassMethods($ctrlName = null) {
  219.         App::import('Controller', $ctrlName);
  220.         if (strlen(strstr($ctrlName, '.')) > 0) {
  221.             // plugin's controller
  222.             $num = strpos($ctrlName, '.');
  223.             $ctrlName = substr($ctrlName, $num+1);
  224.         }
  225.         $ctrlclass = $ctrlName . 'Controller';
  226.         $methods = get_class_methods($ctrlclass);
  227.    
  228.         // Add scaffold defaults if scaffolds are being used
  229.         $properties = get_class_vars($ctrlclass);
  230.         if (array_key_exists('scaffold',$properties)) {
  231.             if($properties['scaffold'] == 'admin') {
  232.                 $methods = array_merge($methods, array('admin_add', 'admin_edit', 'admin_index', 'admin_view', 'admin_delete'));
  233.             } else {
  234.                 $methods = array_merge($methods, array('add', 'edit', 'index', 'view', 'delete'));
  235.             }
  236.         }
  237.         return $methods;
  238.     }
  239.    
  240.     function _isPlugin($ctrlName = null) {
  241.         $arr = String::tokenize($ctrlName, '/');
  242.         if (count($arr) > 1) {
  243.             return true;
  244.         } else {
  245.             return false;
  246.         }
  247.     }
  248.    
  249.     function _getPluginControllerPath($ctrlName = null) {
  250.         $arr = String::tokenize($ctrlName, '/');
  251.         if (count($arr) == 2) {
  252.             return $arr[0] . '.' . $arr[1];
  253.         } else {
  254.             return $arr[0];
  255.         }
  256.     }
  257.    
  258.     function _getPluginName($ctrlName = null) {
  259.         $arr = String::tokenize($ctrlName, '/');
  260.         if (count($arr) == 2) {
  261.             return $arr[0];
  262.         } else {
  263.             return false;
  264.         }
  265.     }
  266.    
  267.     function _getPluginControllerName($ctrlName = null) {
  268.         $arr = String::tokenize($ctrlName, '/');
  269.         if (count($arr) == 2) {
  270.             return $arr[1];
  271.         } else {
  272.             return false;
  273.         }
  274.     }
  275.    
  276.     /**
  277.     * Get the names of the plugin controllers ...
  278.     *
  279.     * This function will get an array of the plugin controller names, and
  280.     * also makes sure the controllers are available for us to get the
  281.     * method names by doing an App::import for each plugin controller.
  282.     *
  283.     * @return array of plugin names.
  284.     *
  285.     */
  286.     function _getPluginControllerNames() {
  287.         App::import('Core', 'File', 'Folder');
  288.         $paths = Configure::getInstance();
  289.         $folder =& new Folder();
  290.         $folder->cd(APP . 'plugins');
  291.    
  292.         // Get the list of plugins
  293.         $Plugins = $folder->read();
  294.         $Plugins = $Plugins[0];
  295.         $arr = array();
  296.    
  297.         // Loop through the plugins
  298.         foreach($Plugins as $pluginName) {
  299.             // Change directory to the plugin
  300.             $didCD = $folder->cd(APP . 'plugins'. DS . $pluginName . DS . 'controllers');
  301.             // Get a list of the files that have a file name that ends
  302.             // with controller.php
  303.             $files = $folder->findRecursive('.*_controller\.php');
  304.    
  305.             // Loop through the controllers we found in the plugins directory
  306.             foreach($files as $fileName) {
  307.                 // Get the base file name
  308.                 $file = basename($fileName);
  309.    
  310.                 // Get the controller name
  311.                 $file = Inflector::camelize(substr($file, 0, strlen($file)-strlen('_controller.php')));
  312.                 if (!preg_match('/^'. Inflector::humanize($pluginName). 'App/', $file)) {
  313.                     if (!App::import('Controller', $pluginName.'.'.$file)) {
  314.                         debug('Error importing '.$file.' for plugin '.$pluginName);
  315.                     } else {
  316.                         /// Now prepend the Plugin name ...
  317.                         // This is required to allow us to fetch the method names.
  318.                         $arr[] = Inflector::humanize($pluginName) . "/" . $file;
  319.                     }
  320.                 }
  321.             }
  322.         }
  323.         return $arr;
  324.     }                                                          
  325.    
  326.     function admin_edit($id = null) {
  327.         if (!$id && empty($this->data)) {
  328.             $this->Session->setFlash(__('Invalid user', true),'default');
  329.             $this->redirect(array('action' => 'index'));
  330.         }
  331.         if (!empty($this->data)) {
  332.             $error = 0;
  333.             if($this->data['User']['password1'] != "") {
  334.                 if($this->data['User']['password1'] == $this->data['User']['password2']) {
  335.                     $this->data['User']['password'] = $this->Auth->password($this->data['User']['password1']);
  336.                 } else {
  337.                     $this->Session->setFlash(__('The passwords provided do no match', true),'default');
  338.                     $error = 1;
  339.                 }
  340.             }
  341.             if ($error == 0 && $this->User->save($this->data)) {
  342.                 $this->Session->setFlash(__('The user has been saved', true),'default');
  343.                 $this->redirect(array('action' => 'index'));
  344.             } else if($error == 0) {
  345.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.', true),'default');
  346.             }
  347.         }
  348.         if (empty($this->data)) {
  349.             $this->data = $this->User->read(null, $id);
  350.             $this->data['User']['password1'] = "";
  351.             $this->data['User']['password2'] = "";
  352.         }
  353.         $groups = $this->User->Group->find('list');
  354.         $this->set(compact('groups'));
  355.     }
  356.    
  357.     function admin_index($id = null) {
  358.         $users = null;
  359.         if(!empty($this->data)) {
  360.             // search function    
  361.             $keywords = $this->data['User']['search'];
  362.             $users = $this->User->query("SELECT *
  363.                                        FROM users AS User
  364.                                        WHERE (User.username LIKE '%" . $keywords . "%'
  365.                                        OR User.firstname LIKE '%" . $keywords . "%'
  366.                                        OR User.lastname LIKE '%" . $keywords . "%'
  367.                                        OR User.nickname LIKE '%" . $keywords . "%')");
  368.         }
  369.         $this->set('users',$users);
  370.     }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement