dongtigai3875 2013-05-05 08:42
浏览 7
已采纳

我的cms中的用户权限

I am developing a cms as my hobby and i got stuck on something....In my mysql db i have different classes of users:Admins, normal users, veterans, premium etc.....Is there a way to create a php file wich contains settings for each user class? And then to use a function or something to check if a certain user has the right to...create a page for example....

For the moment i am checking the users rights with sessions...

  If($_SESSION['user_type']=='Admin'||$_SESSION['user_type']=='premium'){
 //do stuff
 }else if()......... {
  // ..............
  }

but i want something like that

check_user_right(user_name);
if ($can_create_page) == true{
 do......}else{....}
  • 写回答

1条回答 默认 最新

  • doujiazong0322 2013-05-05 08:47
    关注

    First of all, you should know that you should be storing user information in the database. Then, when someone logs in and you verify the login, you can store his or her user ID in the session, and then to get all other user information, like user_type, you would query the database based on that ID. Not sure if you're doing that yet, but you should be if you aren't.

    As far as user rights go, you have two options.

    The OOP Way

    This is the one I recommend. It entails creating a User class that encapsulates all of the logic for retrieving a user from the database and subsequently checking if that user has a specific right.

    class User {
        protected static $_rights = array(
            'admin'=>array(
                'create_page','remove_page', etc...
            )
        );
        public static retrieve($id) {
            // Call to your database or persistent storage to retrieve user info based on id
            return new static($retrieved_user_data);
        }
    
        public function has_right($right) {
            return in_array($right, static::$_rights[$this->user_type]);
        }
    }
    

    The Non-OOP Way

    This is much lower-level and may be better for you in the meantime. Basically you would create an array of rights per user level that you store in the same file as the function you use to check them. Then this function must in turn be included on every page you plan to use it.

    E.g., say you put the function in a file called functions.php, and it looks like this:

    $user_rights = array(
        'admin'=>array('create_page','remove_page','edit_user',...),
        'veteran'=>array('ban_user','edit_page'),
        // Other rights here
    );
    function has_right($user_id, $right) {
        global $user_rights;
        // Retrieve information from persistent storage about the user
        // which I'm assuming you will store in $user_info
        return in_array($right, $user_rights[$user_info['user_type']]);
    }
    

    Then you should include this file on any other file that you want to check user rights, and then all you need is the user ID (Stored in the session after log in) and the user type, which you can get from the database or other persistent storage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等