View file vkclone-0.0.1/protected/models/PublicReader.php

File size: 1.67Kb
<?php
/**
 * This is the model class for table "{{public_readers}}".
 */
class PublicReader extends CActiveRecord {
	const READER = "reader";
	const WRITER = "writer";
	const MODERATOR = "moderator";
	const ADMINISTRATOR = "administrator";

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{public_readers}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array(
			array('public_id, user_id, role', 'required'),
			array('public_id, user_id', 'length', 'max' => 11),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		return array(
			'public' => array(self::BELONGS_TO, 'CPublic', 'public_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 Reader 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::READER,
			1 => self::WRITER,
			2 => self::MODERATOR,
			3 => 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;
	}
}