<?php class EWebUser extends CWebUser{ protected $_model; protected function loadUser() { if ( $this->_model === null ) { $this->_model = User::model()->findByPk($this->id); } return $this->_model; } function getLevel() { $user=$this->loadUser(); if($user) return $user->level_id; return 100; } }The above code CWebUser lower class that exist in Yii. In the above code getLevel function () will return the currently logged on user level from the table "Users" to attribute "level_id". After making the above code, we can restrict certain functions based on levels like the following example:
public function accessRules() { return array( array('allow', 'actions'=>array('index','view'), 'users'=>array('*'), ), array('allow', 'actions'=>array('create','update'), 'users'=>array('@'), ), array('allow', 'actions'=>array('admin','delete'), 'expression'=>'$user->getLevel()<=1', ), array('deny', 'users'=>array('*'), ), ); }The above code allows the index action and views accessible to all the good people who are logged in or not. Create and update action can only be accessed by users who are logged. Admin and delete action can only be accessed by users who have a level value <= 1. Level value itself will be taken from the user table. In this application level_id attribute will be connected to the attribute level that existed at the table level and level 1 a user with admin type. Now, using these methods you can define your own rules of access levels each. For this application we would have to adjust to the needs of our application.
In addition to set an access level, we can also use to set the user access level access to a view. For example if login with admin level would show made A, whereas if login with normal user level would show made B. Here's an example of its use
if(Yii::app()->user->getLevel()<=2) { echo "it"; } else { echo "this"; }
Will you continue this tutorial series?
ReplyDeleteI am so sorry, i'm very busy in my real life, maybe next time i will continue, thank you for visiting.. :)
DeleteReally appreciate you sharing this article post.Thanks Again. Will read on... Application Development using Angularjs
ReplyDeleteKocaeli
ReplyDeleteDenizli
Bartın
Kocaeli
Adana
YGR
whatsapp görüntülü show
ReplyDeleteücretli.show
İBY7M
3D8EB
ReplyDeleteyalova ücretsiz sohbet siteleri
ardahan mobil sohbet odaları
çanakkale rastgele canlı sohbet
yabancı görüntülü sohbet
telefonda rastgele sohbet
sesli sohbet
görüntülü sohbet kızlarla
çankırı rastgele sohbet odaları
yabancı sohbet
افضل شركة تسليك مجاري بالاحساء 6GI24fbQZY
ReplyDeleteتسليك مجاري بالاحساء hwAXleTKDT
ReplyDeleteشركة تنظيف خزانات hzlq9VS0Vo
ReplyDeleteشركة تسليك مجاري بالاحساء Rwyldd2kxz
ReplyDeleteشركة مكافحة النمل الابيض بالدمام RvQ6289Urs
ReplyDelete