主页 > 编程 > php >

php web通用权限管理之 角色 设置权限代码

2019-07-17 14:35 阅读:180 来源:智宇SEO自媒体
php web通用权限管理之 角色 设置权限代码
 public function setaccess()
    {
        $id = $this->gets['1'];
        $this->roleData = $this->db->where('id = ?', array($id))->fetch();
        $this->accessdb = db('access');
        $this->accessdata = $this->accessdb->where('status=?', array(1))->fetchAll();

        $this->role_accessdb = db('role_access');

        $this->access_id = $this->role_accessdb->where('role_id=?', array($id))->fetchAll('access_id');
        //二维数组降为一维数组
        $this->aids = array_column($this->access_id, 'access_id');

        if (!empty($_POST['btn'])) {

            if (isset($_POST['accessid'])) {

                $accessid = $_POST['accessid'];
                $rid = implode(',', $accessid);
                $aid = explode(',', $rid);//从数据库取出数据时用。


                if (count($this->aids) <= 0)//第一次添加
                {
                    foreach ($aid as $xid) {
                        $role_access_Data = array('role_id' => $id,
                            'access_id' => $xid,
                            'created_time' => date("Y-m-d H:i:s"),
                        );
                        $role_access_id[] = $this->role_accessdb->add($role_access_Data);
                    }

                    if (count($role_access_id) == count($aid)) {
                        s('role', 'index', '角色权限设置成功');
                    }

                } else {
                    //数据库里access_id 和复选框选中的对比差集
                    $result1 = array_diff($this->aids, $aid);
                    if (!empty($result1))//比原来少选权限
                    {
                        //echo "比原来少选了";
                        if ($result1 != $this->aids) {
                            foreach ($result1 as $r) {
                                $this->role_accessdb->where('role_id = ? and access_id=?', array($id, $r))->delete();

                            }
                            $res = array_diff($aid, $this->aids);
                            foreach ($res as $r) {
                                $role_accessData = array('role_id' => $id,
                                    'access_id' => $r,
                                    'created_time' => date("Y-m-d H:i:s"),
                                );
                                $user_role_id[] = $this->role_accessdb->add($role_accessData);
                            }


                        } else {
                            //echo "没有交集了";
                            foreach ($result1 as $r) {
                                $this->role_accessdb->where('role_id = ? and access_id=?', array($id, $r))->delete();


                            }

                            foreach ($aid as $a) {
                                $role_accessData = array('role_id' => $id,
                                    'access_id' => $a,
                                    'created_time' => date("Y-m-d H:i:s"),
                                );
                                $user_role_id[] = $this->role_accessdb->add($role_accessData);
                            }


                        }

                    } else {
                        if ($aid != $this->aids)//比原来多选权限
                        {
                            $result2 = array_diff($aid, $this->aids);
                            //echo "比原来多选了";
                            if ($result2) {
                                foreach ($result2 as $rid) {
                                    $role_accessData = array('role_id' => $id,
                                        'access_id' => $rid,
                                        'created_time' => date("Y-m-d H:i:s"),
                                    );
                                    $user_role_id[] = $this->role_accessdb->add($role_accessData);

                                }

                            }

                        }
                    }


                    s('role', 'index', '角色权限设置成功');


                }


            } else {
                //都不选,全部删除
                if (isset($this->access_id)) {
                    $this->role_accessdb->where('role_id = ?', array($id))->delete();
                    s('role', 'index', '角色权限设置成功');
                }

            }


        } else {
            $this->display('role_accss.php');
        }

    }