Guest User

CakePHP code with many bugs

a guest
Jul 1st, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.82 KB | None | 0 0
  1. <!-- User model -->
  2. <?php
  3.  
  4. class User extends AppModel {
  5.     var $name = 'User';
  6.    
  7.     var $belongsTo = array (
  8.         'Group'
  9.     );
  10.    
  11.     var $hasMany = array (
  12.         'UserLog',
  13.         'WorkLog'
  14.     );
  15.    
  16.     var $actAs = array (
  17.         'Acl' => array ('type' => 'requester')
  18.     );
  19.    
  20.     function parentNode () {
  21.         if (!$this->id && empty ($this->data)) {
  22.             return null;
  23.         }
  24.        
  25.         if (isset ($this->data['User']['group_id'])) {
  26.             $groupId = $this->data['User']['group_id'];
  27.         } else {
  28.             $groupId = $this->field ('group_id');
  29.         }
  30.        
  31.         if (!$groupId) {
  32.             return null;
  33.         } else {
  34.             return array (
  35.                 'Group' => array ('id' => $groupId)
  36.             );
  37.         }
  38.     }
  39. }
  40.  
  41. ?>
  42.  
  43. <!-- Group Model -->
  44.  
  45. <?php
  46.  
  47. class Group extends AppModel {
  48.     var $name = 'Group';
  49.    
  50.     var $hasMany = array (
  51.         'User'
  52.     );
  53.    
  54.     var $actAs = array (
  55.         'Acl' => array ('type' => 'requester')
  56.     );
  57.    
  58.     function parentNode () {
  59.         return null;
  60.     }
  61. }
  62.  
  63. ?>
  64.  
  65. <!-- Users Controller -->
  66.  
  67. <?php
  68.  
  69. class UsersController extends AppController {
  70.     var $name = 'Users';
  71.    
  72.     function index() {
  73.         $this->User->recursive = 0;
  74.         $this->set('users', $this->paginate());
  75.     }
  76.    
  77.     function beforeFilter () {
  78.         parent::beforeFilter();
  79.        
  80.         /* FIXME: Temporal code */
  81.         $this->Auth->allow (array ('*'));
  82.     }
  83.    
  84.     public function login () {
  85.        
  86.     }
  87.    
  88.     public function logout () {
  89.         $this->Auth->logout();
  90.         $this->redirect('/');
  91.     }
  92.    
  93.     public function dashboard () {
  94.        
  95.     }
  96.  
  97.     function view($id = null) {
  98.         if (!$id) {
  99.             $this->Session->setFlash(__('Invalid user', true));
  100.             $this->redirect(array('action' => 'index'));
  101.         }
  102.         $this->set('user', $this->User->read(null, $id));
  103.     }
  104.  
  105.     function add() {
  106.         if (!empty($this->data)) {
  107.             $this->User->create();
  108.             if ($this->User->save($this->data)) {
  109.                 $this->Session->setFlash(__('The user has been saved', true));
  110.                 $this->redirect(array('action' => 'index'));
  111.             } else {
  112.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
  113.             }
  114.         }
  115.         $groups = $this->User->Group->find('list');
  116.         $this->set(compact('groups'));
  117.     }
  118.  
  119.     function edit($id = null) {
  120.         if (!$id && empty($this->data)) {
  121.             $this->Session->setFlash(__('Invalid user', true));
  122.             $this->redirect(array('action' => 'index'));
  123.         }
  124.         if (!empty($this->data)) {
  125.             if ($this->User->save($this->data)) {
  126.                 $this->Session->setFlash(__('The user has been saved', true));
  127.                 $this->redirect(array('action' => 'index'));
  128.             } else {
  129.                 $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
  130.             }
  131.         }
  132.         if (empty($this->data)) {
  133.             $this->data = $this->User->read(null, $id);
  134.         }
  135.         $groups = $this->User->Group->find('list');
  136.         $this->set(compact('groups'));
  137.     }
  138.  
  139.     function delete($id = null) {
  140.         if (!$id) {
  141.             $this->Session->setFlash(__('Invalid id for user', true));
  142.             $this->redirect(array('action'=>'index'));
  143.         }
  144.         if ($this->User->delete($id)) {
  145.             $this->Session->setFlash(__('User deleted', true));
  146.             $this->redirect(array('action'=>'index'));
  147.         }
  148.         $this->Session->setFlash(__('User was not deleted', true));
  149.         $this->redirect(array('action' => 'index'));
  150.     }
  151. }
  152.  
  153. <!-- Groups Controller -->
  154. <?php
  155.  
  156. class GroupsController extends AppController {
  157.     var $name = 'Groups';
  158.    
  159.     function beforeFilter () {
  160.         parent::beforeFilter();
  161.        
  162.         /* FIXME: Temporal code */
  163.         $this->Auth->allow (array ('*'));
  164.     }
  165.  
  166.     function index() {
  167.         $this->Group->recursive = 0;
  168.         $this->set('groups', $this->paginate());
  169.     }
  170.  
  171.     function view($id = null) {
  172.         if (!$id) {
  173.             $this->Session->setFlash(__('Invalid group', true));
  174.             $this->redirect(array('action' => 'index'));
  175.         }
  176.         $this->set('group', $this->Group->read(null, $id));
  177.     }
  178.  
  179.     function add() {
  180.         if (!empty($this->data)) {
  181.             $this->Group->create();
  182.             if ($this->Group->save($this->data)) {
  183.                 $this->Session->setFlash(__('The group has been saved', true));
  184.                 $this->redirect(array('action' => 'index'));
  185.             } else {
  186.                 $this->Session->setFlash(__('The group could not be saved. Please, try again.', true));
  187.             }
  188.         }
  189.     }
  190.  
  191.     function edit($id = null) {
  192.         if (!$id && empty($this->data)) {
  193.             $this->Session->setFlash(__('Invalid group', true));
  194.             $this->redirect(array('action' => 'index'));
  195.         }
  196.         if (!empty($this->data)) {
  197.             if ($this->Group->save($this->data)) {
  198.                 $this->Session->setFlash(__('The group has been saved', true));
  199.                 $this->redirect(array('action' => 'index'));
  200.             } else {
  201.                 $this->Session->setFlash(__('The group could not be saved. Please, try again.', true));
  202.             }
  203.         }
  204.         if (empty($this->data)) {
  205.             $this->data = $this->Group->read(null, $id);
  206.         }
  207.     }
  208.  
  209.     function delete($id = null) {
  210.         if (!$id) {
  211.             $this->Session->setFlash(__('Invalid id for group', true));
  212.             $this->redirect(array('action'=>'index'));
  213.         }
  214.         if ($this->Group->delete($id)) {
  215.             $this->Session->setFlash(__('Group deleted', true));
  216.             $this->redirect(array('action'=>'index'));
  217.         }
  218.         $this->Session->setFlash(__('Group was not deleted', true));
  219.         $this->redirect(array('action' => 'index'));
  220.     }
  221. }
  222.  
  223. ?>
  224.  
  225. <!-- App Controller -->
  226. <?php
  227.  
  228. class AppController extends Controller {
  229.     var $components = array ('Acl', 'Auth', 'Session');
  230.     var $helpers = array ('Html', 'Form', 'Session');
  231.    
  232.     function beforeFilter () {
  233.         $this->Auth->loginAction = array (
  234.             'controller' => 'users',
  235.             'action' => 'login'
  236.         );
  237.        
  238.         $this->Auth->autoRedirect = array (
  239.             'controller' => 'users',
  240.             'action' => 'dashboard'
  241.         );
  242.        
  243.         $this->Auth->loginRedirect = array (
  244.             'controller' => 'users',
  245.             'action' => 'dashboard'
  246.         );
  247.        
  248.         $this->Auth->logoutRedirect = array (
  249.             'controller' => 'users',
  250.             'action' => 'logout'
  251.         );
  252.        
  253.         $this->actionPath = 'controllers/';
  254.        
  255.         /* FIXME: important read about ACL ( http://api.cakephp.org/class/auth-component )  */
  256.         $this->authorize = 'actions';
  257.     }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment