01.22 php saved
t73net
Tags add more
ACL, Groups, Users, auth and habtm  
Note
I need users and groups to be relational in that a single user may have multiple groups and each group will have multiple users.

Is using a HABTM model structure appropriate here?

I have followed the outline of http://realm3.com/articles/setting_up_user_groups_with_acl_and_auth_in_cakephp_1.2
and have not come up with a solution for the parent_node function in GroupsModel or UsersModel
  1. /**
  2. * SQL Table Structure Dump
  3. *
  4. --
  5. -- Table structure for table `acos`
  6. --
  7.  
  8. CREATE TABLE `acos` (
  9.   `id` int(11) NOT NULL auto_increment,
  10.   `parent_id` int(11) default NULL,
  11.   `model` varchar(255) collate utf8_bin default NULL,
  12.   `foreign_key` int(11) default NULL,
  13.   `alias` varchar(255) collate utf8_bin default NULL,
  14.   `lft` int(11) default NULL,
  15.   `rght` int(11) default NULL,
  16.   PRIMARY KEY  (`id`)
  17. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3 ;
  18.  
  19.  
  20. --
  21. -- Table structure for table `aros`
  22. --
  23.  
  24. CREATE TABLE `aros` (
  25.   `id` int(11) NOT NULL auto_increment,
  26.   `parent_id` int(11) default NULL,
  27.   `model` varchar(255) collate utf8_bin default NULL,
  28.   `foreign_key` int(11) default NULL,
  29.   `alias` varchar(255) collate utf8_bin default NULL,
  30.   `lft` int(11) default NULL,
  31.   `rght` int(11) default NULL,
  32.   PRIMARY KEY  (`id`)
  33. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=11 ;
  34.  
  35.  
  36. --
  37. -- Table structure for table `aros_acos`
  38. --
  39.  
  40. CREATE TABLE `aros_acos` (
  41.   `id` int(11) NOT NULL auto_increment,
  42.   `aro_id` int(11) default NULL,
  43.   `aco_id` int(11) default NULL,
  44.   `_create` int(11) NOT NULL default '0',
  45.   `_read` int(11) NOT NULL default '0',
  46.   `_update` int(11) NOT NULL default '0',
  47.   `_delete` int(11) NOT NULL default '0',
  48.   PRIMARY KEY  (`id`)
  49. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=2 ;
  50.  
  51. --
  52. -- Table structure for table `groups`
  53. --
  54.  
  55. CREATE TABLE `groups` (
  56.   `id` smallint(5) unsigned NOT NULL auto_increment,
  57.   `name` varchar(50) collate utf8_bin default NULL,
  58.   `description` text collate utf8_bin,
  59.   `parent_id` int(11) NOT NULL,
  60.   PRIMARY KEY  (`id`)
  61. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=10 ;
  62.  
  63. --
  64. -- Table structure for table `groups_users`
  65. --
  66.  
  67. CREATE TABLE `groups_users` (
  68.   `id` int(10) unsigned NOT NULL auto_increment,
  69.   `group_id` int(10) unsigned NOT NULL,
  70.   `user_id` int(10) unsigned NOT NULL,
  71.   `created` datetime default NULL,
  72.   `modified` datetime default NULL,
  73.   PRIMARY KEY  (`id`),
  74.   KEY `group_id` (`group_id`,`user_id`)
  75. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Group User HABTM Relational Table' AUTO_INCREMENT=2 ;
  76.  
  77. --
  78. -- Table structure for table `users`
  79. --
  80.  
  81. CREATE TABLE `users` (
  82.   `id` int(10) unsigned NOT NULL auto_increment,
  83.   `username` varchar(250) collate utf8_bin NOT NULL default '',
  84.   `password` varchar(250) collate utf8_bin NOT NULL default '',
  85.   `active` tinyint(1) unsigned NOT NULL default '1',
  86.   `created` datetime default NULL,
  87.   `modified` datetime default NULL,
  88.   PRIMARY KEY  (`id`),
  89.   UNIQUE KEY `username` (`username`)
  90. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='User Data Information' AUTO_INCREMENT=2 ;
  91.  
  92. */
  93.  
  94. /**
  95. * /app/models/users.php
  96. */
  97. <?php
  98. class User extends AppModel {
  99.  
  100.     var $name = 'User';
  101.     var $actsAs = array( 'Acl'=>'requester' );
  102.     var $displayField = 'username';
  103.     var $validate = array(
  104.         'id' => array(
  105.             'rule' => 'blank',
  106.             'on' => 'create'
  107.         ),
  108.         'username' => array(
  109.             'checkUnique' => array(
  110.                 'rule' => array('checkUnique', 'username'),
  111.                 'message' => 'An account with that username has already been registered.',
  112.                 'on' => 'create'
  113.             )
  114.         ),
  115.         'password' => array(
  116.             'alphanumeric' => array(
  117.                 'rule' => 'alphaNumeric'
  118.                 'message' => 'Only alphabets and numbers allowed',
  119.                 'on' => 'create'
  120.             ),
  121.             'between' => array(
  122.                 'rule' => array('between', 5, 15),
  123.                 'message' => 'Between 5 to 15 characters'
  124.             )
  125.         ),
  126.         'confirmpassword' => array(
  127.             'alphanumeric' => array(
  128.                 'rule' => 'alphaNumeric'
  129.                 'message' => 'Only alphabets and numbers allowed',
  130.                 'on' => 'create'
  131.             ),
  132.             'between' => array(
  133.                 'rule' => array('between', 5, 15),
  134.                 'message' => 'Between 5 to 15 characters'
  135.             )
  136.         )
  137.     );
  138.  
  139.     var $hasAndBelongsToMany = array(
  140.         'Group' =>
  141.             array('className'      => 'Group',
  142.             'joinTable'    => 'groups_users',
  143.             'foreignKey'            => 'group_id',
  144.             'associationForeignKey' => 'user_id',
  145.             'conditions'            => '',
  146.             'order'     => '',
  147.             'limit'     => '',
  148.             'uniq'          => true,
  149.             'finderQuery'         => '',
  150.             'deleteQuery'         => '',
  151.             'insertQuery'         => ''
  152.         )
  153.     );
  154.        
  155.     function checkUnique($data, $fieldName) {
  156.         $valid = false;
  157.         if(isset($fieldName) && $this->hasField($fieldName))
  158.         {
  159.             $valid = $this->isUnique(array($fieldName => $data));
  160.         }
  161.         return $valid;
  162.     }
  163.  
  164.     function parentNode( ) {
  165.         if (!$this->id) {
  166.             return null;
  167.         }
  168.         $data = $this->read();
  169.         if (!$data['User']['group_id']){
  170.             return null;
  171.         } else {
  172.             return array('model' => 'Group', 'foreign_key' => $data['User']['group_id']);
  173.         }
  174.     }
  175.  
  176.     function validLogin($data)
  177.     {
  178.  
  179.         $user = $this->find(array('username' => $data['User']['username'], 'password' => ($data['User']['password'])), array('id','username', 'password'));
  180.         if(!empty($user)){
  181.             $this->user = $user['User'];
  182.             return TRUE;
  183.         }
  184.         else
  185.         {
  186.             return FALSE;
  187.         }
  188.     }
  189. }
  190. ?>
  191.  
  192. /**
  193. * /app/models/groups.php
  194. */
  195. <?php
  196. class Group extends AppModel {
  197.     var $name = 'Group';
  198.     var $actsAs = array('Acl'=>'requester');
  199.  
  200.     var $hasAndBelongsToMany = array(
  201.         'User' =>
  202.             array('className'      => 'User',
  203.             'joinTable'    => 'groups_users',
  204.             'foreignKey'            => 'user_id',
  205.             'associationForeignKey' => 'group_id',
  206.             'conditions'            => '',
  207.             'order'     => '',
  208.             'limit'     => '',
  209.             'uniq'          => true,
  210.             'finderQuery'         => '',
  211.             'deleteQuery'         => '',
  212.             'insertQuery'         => ''
  213.         )
  214.     );
  215.        
  216.     function parentNode()
  217.     {
  218.         if (!$this->id)
  219.         {
  220.             return null;
  221.         }
  222.         $data = $this->read();
  223.         if (!$data['Group']['parent_id'])
  224.         {
  225.             return null;
  226.         }
  227.         else
  228.         {
  229.             return $data['Group']['parent_id'];
  230.         }
  231.     }
  232. }
  233.  
  234. ?>
Parsed in 0.267 seconds, using GeSHi 1.0.7.14

Modify this Paste