Yii 1.1: shortcut/usefull access roles methods RBAC


These are functions that I used very often, may helps you!

Create a new SYii.php file in protected/componets folder

Class SYii {
    //check if a specific user id is the current user
    public static function checkifMe($userid) {
        return Yii::app()->user->id == $userid; 
    //check if a member
    public static function checkifMember($userid) {
        return Yii::app()->authManager->isAssigned("member", $userid);
    public static function checkifAdmin($userid) {
        return Yii::app()->authManager->isAssigned("admin", $userid);
    public static function checkifSuperAdmin($userid) {
        return Yii::app()->authManager->isAssigned("superadmin", $userid);
    //check if user is a specific user type like admin,member,supradmin,anotherrole etc
    public static function checkifitis($userid,$role) {
        return Yii::app()->authManager->isAssigned($role, $userid);
    //returns all roles of RBAC as list
    public static function rolesList() {
        $roles = Yii::app()->authManager->getRoles();
        $list = array();
        foreach ($roles as $key => $val) {
            $list[] = $key;
        return $list;
    //returns all the roles of the current user as list
    public static function rolesListPermit() {
        $list = self::rolesList();
        $res = array();
        foreach ($list as $key => $val) {
            if (Yii::app()->user->checkAccess($val))
                $res[$key] = $val;
        return $res;
    //returns the lowest permission role of the RBAC
      public static function getAuthItemLower() {
            $roles = Yii::app()->authManager->getRoles();
            foreach ($roles as $key => $val) {
                $is_lower_role = true;
                $roles = Yii::app()->authManager->getItemChildren($key);
                if (is_array($roles)) {
                    foreach ($roles as $key2 => $val) {
                        if ($val->type == CAuthItem::TYPE_ROLE) {
                            $is_lower_role = false;
                } else {
                    if ($roles->type == CAuthItem::TYPE_ROLE)
                        $is_lower_role = false;
                if ($is_lower_role) {
                    return $key;
            return null;

Total 5 comments

#17288 report it
Kostas Apazidis (KonApaz) at 2014/05/19 03:25pm
RE: Shorty(Short Yii)

As I mentioned there are similar sortly Classes like the qiang class


I suggested even more

So, we could integrate all of them in one class :)

#17283 report it
Ali MasudianPour at 2014/05/19 11:49am
Shorty(Short Yii)

I have something similar to the following article but a little more comprehensive. Shorty (Short Yii) Shorty Class

#17280 report it
Kostas Apazidis (KonApaz) at 2014/05/19 09:19am
Why I add new class?

according to this http://www.yiiframework.com/wiki/31/use-shortcut-functions-to-reduce-typing/

qiang suggest us to reduce the typing of common methods, so I write the methods in seperated class :)

#17278 report it
Kostas Apazidis (KonApaz) at 2014/05/19 09:15am
Re: bad practice

nineinchnick, I agree with you

The main reason that I added in new Class is to use sorting code, for example


instead of

#17274 report it
nineinchnick at 2014/05/19 06:50am
bad practice

Such methods should be added to classes extending CAuthManager and CWebUser, not a god-like class.

Leave a comment

Please to leave your comment.

Write new article