11.15
php
saved
gwoo
Note
latest version of rdAuth
latest version of rdAuth
- <?php
- /*
- * rdAuth Component
- *
- * @author gwoo <gwoo@rd11.com>
- * @version 1.1.10.3825
- * @license CSFL
- */
- class rdAuthComponent extends Object {
- /**
- * the name for the cooke.
- *
- * @var string
- * @access public
- */
- var $cookie = '__rd';
- /**
- * model that stores the role field
- *
- * @var unknown_type
- * @access private
- */
- var $model = 'User';
- /**
- * the redirect if auth fails.
- *
- * @var string
- * @access public
- */
- var $loginPage = '/users/login';
- /**
- * current url.
- *
- * @var string
- * @access private
- */
- var $currentUrl = '/';
- /**
- * referring url.
- *
- * @var string
- * @access private
- */
- var $referer = '/';
- /**
- * special admin roles
- *
- * @var array
- * @access public
- */
- /**
- * all roles available
- *
- * @var array
- * @access public
- */
- /**
- * id of the logged in user
- *
- * @var unknown_type
- * @access private
- */
- var $id = null;
- /**
- * username of the logged in user
- *
- * @var string
- * @access private
- */
- var $username = null;
- /**
- * role assigned to the logged in user
- *
- * @var string
- * @access private
- */
- var $role = null;
- /**
- * if role passes users set allowed
- *
- * @var boolean
- * @access private
- */
- var $allow = null;
- /**
- * Valid if role, username , and id exist.
- *
- * @var string
- * @access public
- */
- var $valid = false;
- /**
- * Error messages to be displayed if the user is short of access for the requested action.
- *
- * @var string
- * @access private
- */
- var $errors = null;
- /**
- * hash used in basic encryption of session values.
- *
- * @var string
- * @access private
- */
- var $__hash = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
- /**
- * if admin route then __cakeAdmin equals params['admin']
- *
- * @var string
- * @access private
- */
- var $__cakeAdmin = null;
- /**
- * Uses callback to automatically check rights
- * @return redirect if check fails
- */
- function startup(&$controller)
- {
- $this->currentUrl = '/'.$controller->params['url']['url'];
- }
- $this->__cakeAdmin = $controller->params['admin'];
- }
- }
- //setup current session
- $this->set();
- {
- $this->referer = $this->currentUrl;
- $this->Session->write("{$this->cookie}.referer", $this->referer);
- }
- if($this->check($controller->action, $vars['rdAccess']) === true)
- {
- foreach($this->roles as $role) {
- if($roles[$this->role] <= $roles[$role]) {
- $this->{low($role)} = 1;
- }
- }
- $this->valid = 1;
- $this->allow = 1;
- }
- }
- else
- {
- $controller->redirect($this->loginPage);
- exit;
- }
- }
- }
- {
- foreach($data as $key=>$value) {
- }
- } else {
- }
- }
- {
- foreach($data as $key=>$value) {
- $data[$key] = $string[0];
- }
- } else {
- $data = $string[0];
- }
- }
- /**
- * Function to check the session and return local vars
- *
- * @param string $data used for login method
- * @return errors
- */
- function set($data='')
- {
- foreach($this->roles as $role) {
- $this->{low($role)} = null;
- }
- $this->referer = $this->Session->read("{$this->cookie}.referer");
- {
- $this->encrypt($data);
- $this->Session->write("{$this->cookie}", $data);
- $this->Session->del("{$this->cookie}.referer");
- }
- if($this->Session->check("{$this->cookie}.id") && $this->Session->valid("{$this->cookie}.id") &&
- $this->Session->check("{$this->cookie}.username") && $this->Session->valid("{$this->cookie}.username") &&
- $this->Session->check("{$this->cookie}.role") && $this->Session->valid("{$this->cookie}.role"))
- {
- $data = $this->Session->read("{$this->cookie}");
- $this->decrypt($data);
- $this->id = $data['id'];
- $this->username = $data['username'];
- $this->role = $data['role'];
- }
- elseif($this->Session->error())
- {
- return $this->Session->error();
- }
- return true;
- }
- /**
- * logout method deletes session
- *
- * @return errors
- */
- function logout()
- {
- $this->Session->del("{$this->cookie}.id");
- $this->Session->del("{$this->cookie}.username");
- $this->Session->del("{$this->cookie}.role");
- $this->Session->del("{$this->cookie}");
- if($this->Session->error())
- {
- return $this->Session->error();
- }
- }
- /**
- * Function to return the array of set values
- *
- * @param string $type The array to return
- * @return boolean
- */
- function values() {
- 'valid'=> $this->valid, 'allow'=> $this->allow, 'roles'=> $this->roles);
- foreach($this->roles as $role) {
- $values[low($role)] = 1;
- } else {
- $values[low($role)] = null;
- }
- }
- return $values;
- }
- /**
- * Function to return the roles used by the class
- * types : (admin|null) admin returns only admins, null returns all roles
- * @param string $type The array to return
- * @return $roles
- */
- function roles($type = null) {
- if(low($type) == 'admin') {
- } else {
- }
- }
- /**
- * Function to check the access for the action based on the access list
- *
- * @param string $action The action for which we need to check the access
- * @param array $access Access array for the controller's actions
- * @return boolean
- */
- function check($action, $access = null)
- {
- return true;
- } else {
- return true;
- }
- return true;
- }
- return false;
- } else if($access === '*' || $access === CAKE_ADMIN) {
- if($this->valid && $this->allow) {
- return true;
- }
- return false;
- }
- return true;
- }
- /**
- * Function to check if a access is authorized on a row in the database
- * your table would have a field that contains the role just like the user table
- *
- * @param string $item The field for which we need to check the access
- * @return boolean
- */
- function perm($item = null)
- {
- {
- {
- {
- return true;
- }
- {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Function to generate the conditons to be used in a query assumes a role field
- * in a model specified by var $model
- *
- * @param array $roles The roles to include in the query
- * @return string $condtions
- */
- {
- }
- }
- $combined = array_combine($roles, $roles);
- $roles = am($this->admins, $combined);
- $conditions = null;
- $i = 0;
- foreach($roles as $role)
- {
- if($this->perm($role)) {
- if($i == 0)
- $conditions .= $this->model.".role = '{$role}'";
- else
- $conditions .= ' OR '.$this->model.".role = '{$role}'";
- $i++;
- }
- }
- return $conditions;
- }
- /**
- * Function to store the hits on a particular link in a session relatd to the user
- *
- * @param array $id The unique identifier
- * @example mark a hit on /posts/view/1, use posts.1 as the id
- * @return boolean
- */
- function hit($id, $key = 'hits')
- {
- if($this->Session->check("{$this->cookie}.{$key}"))
- {
- $hits = $this->Session->read("{$this->cookie}.{$key}");
- }
- {
- $this->Session->write("{$this->cookie}.{$key}", $hits);
- return true;
- }
- return false;
- }
- function clearHits($key = 'hits')
- {
- $this->Session->del("{$this->cookie}.{$key}");
- }
- }
- ?>
Parsed in 0.452 seconds, using GeSHi 1.0.7.14