<?php
/**
* This is the model class for table "{{group_members}}".
*/
class GroupMember extends CActiveRecord {
const MEMBER = "member";
const MODERATOR = "moderator";
const ADMINISTRATOR = "administrator";
/**
* @return string the associated database table name
*/
public function tableName() {
return '{{group_members}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
return array(
array('user_id, group_id, role', 'required'),
array('user_id, group_id', 'length', 'max' => 10),
array('role', 'in', 'range' => array(self::MEMBER, self::MODERATOR, self::ADMINISTRATOR)),
);
}
/**
* @return array relational rules.
*/
public function relations() {
return array(
'group' => array(self::BELONGS_TO, 'Group', 'group_id'),
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
);
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return GroupMember the static model class
*/
public static function model($className=__CLASS__) {
return parent::model($className);
}
static public function checkAccess($assignedRole, $requiredRole) {
static $roles = array(
0 => self::MEMBER,
1 => self::MODERATOR,
2 => self::ADMINISTRATOR,
);
$assignedRole = array_search($assignedRole, $roles);
$requiredRole = array_search($requiredRole, $roles);
if ($assignedRole === false || $requiredRole === false)
throw new RuntimeException("Invalid roles passed to ".__CLASS__."::checkAccess() '".var_export(func_get_args(), true)."'");
return $assignedRole >= $requiredRole;
}
}