<?php

namespace auth;

class scope
{
    /**
     * 显示权限信息
     * @param $scope
     * @return mixed
     */
    public function see($scope){
        global $authsql;
        $scopes=$authsql->select("oauth_scopes",'*',[
            "scope"=>$scope
        ]);
        return $scopes[0];
    }

    /**
     * 权限列表
     * @return string
     */
    public function authlist(){
        global $authsql;
        $info=$authsql->select("oauth_scopes",'*');
        $infonum=count($info);
        $appinfo='';
        for ($i=0;$i<$infonum;$i++){
            $appinfo=$appinfo.'<tr><td>
                <input type="checkbox" /></td>
                <td>'.$info[$i]['id'].'</td>
                <td>'.$info[$i]['scope'].'</td>
                <td>'.$info[$i]['name'].'</td>
                <td>'.$this->def($info[$i]['is_default']).'</td>
                <td>
                <div class="am-btn-toolbar">
                <div class="am-btn-group am-btn-group-xs">
                <a href="./scopenew.html?m=edit&scope='.$info[$i]['scope'].'" class="am-btn am-btn-default am-btn-xs am-text-secondary"><span class="am-icon-pencil-square-o"></span> 编辑</a>';
            $appinfo=$appinfo.'</div></div></td></tr>
';
        }
        return $appinfo;
    }
    private function def($v){
        if($v==1){
            return '是';
        }else{
            return "否";
        }
    }

    /**
     * 权限注册
     * @param $scope
     * @param $name
     * @param $def
     * @return array|false|mixed|string
     */
    public function reg($scope,$name,$def=0){
       global $authsql;
       return $authsql->insert("oauth_scopes",[
           "scope"=>$scope,
           "is_default"=>$def,
           "name"=>$name
       ]);
    }

    /**
     * 引用更新
     * @param $scope
     * @param $name
     * @param $def
     * @return false|int
     */
    public function edit($scope,$name,$def){
        global $authsql;
        if (!empty($def)){
            return $authsql->update("oauth_scopes",[
                "is_default"=>$def,
                "name"=>$name
            ],[
                "scope"=>$scope
            ]);
        }else{
            return $authsql->update("oauth_scopes",[
                "name"=>$name
            ],[
                "scope"=>$scope
            ]);
        }
    }
}