权限管理

Posted dangrui0725

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了权限管理相关的知识,希望对你有一定的参考价值。

https://www.cnblogs.com/alex3714/articles/5535652.html

想对一个功能实现权限控制,要做到只能过在views方法上加一个装饰器就行了,比如:

@check_permission
@login_required
def customer_detail(request,customer_id):
    customer_obj = models.Customer.objects.get(id=customer_id)
    customer_form = forms.CustomerDetailForm(instance=customer_obj)
 
    if request.method == POST:
        customer_form = forms.CustomerDetailForm(request.POST,instance=customer_obj)
        if customer_form.is_valid():
            customer_form.save()
            parent_base_url = /.join(request.path.split(/)[:-2])
            print("url:",parent_base_url )
            return  redirect(parent_base_url)
        else:
            print(customer_form.errors)
    return  render(request,crm/customer_detail.html,{customer_form:customer_form})

# 50行实现细粒度的权限控制
#_*_coding:utf-8_*_
__author__ = Alex Li
from django.core.urlresolvers import resolve  # resolve 可将实际url对应到设置的别名
from django.shortcuts import render,redirect

perm_dic = {    # 定义权限字典
    view_customer_list: [customer_list,GET,[]],  # [url别名,GET|POST , 参数]
    view_customer_info: [customer_detail,GET,[]],
    edit_own_customer_info: [customer_detail,POST,[test]],
}

def perm_check(*args,**kwargs):
    request = args[0]
    url_resovle_obj = resolve(request.path_info)
    current_url_namespace = url_resovle_obj.url_name  # 获取url别名(namespace)
    #app_name = url_resovle_obj.app_name #use this name later
    print("url namespace:",current_url_namespace)
    matched_flag = False # find matched perm item  # 初始标志为false
    matched_perm_key = None  
    if current_url_namespace is not None:#if didn‘t set the url namespace, permission doesn‘t work
        print("find perm...")
        for perm_key in perm_dic:
            perm_val = perm_dic[perm_key]
            if len(perm_val) == 3:#otherwise invalid perm data format
                url_namespace,request_method,request_args = perm_val
                print(url_namespace,current_url_namespace)
                if url_namespace == current_url_namespace: #matched the url
                    if request.method == request_method:#matched request method
                        if not request_args:#if empty , pass
                            matched_flag = True
                            matched_perm_key = perm_key
                            print(mtched...)
                            break #no need looking for  other perms
                        else:
                            for request_arg in request_args: #might has many args
                                request_method_func = getattr(request,request_method) #get or post mostly
                                #print("----->>>",request_method_func.get(request_arg))
                                if request_method_func.get(request_arg) is not None:
                                    matched_flag = True # the arg in set in perm item must be provided in request data
                                else:
                                    matched_flag = False
                                    print("request arg [%s] not matched" % request_arg)
                                    break #no need go further
                            if matched_flag == True: # means passed permission check ,no need check others
                                print("--passed permission check--")
                                matched_perm_key = perm_key
                                break

    else:#permission doesn‘t work
        return True

    if matched_flag == True:
        #pass permission check
        perm_str = "crm.%s" %(matched_perm_key)
        if request.user.has_perm(perm_str):
            print("33[42;1m--------passed permission check----33[0m")
            return True
        else:
            print("33[41;1m ----- no permission ----33[0m")
            print(request.user,perm_str)
            return False
    else:
        print("33[41;1m ----- no matched permission  ----33[0m")



def check_permission(func):    # 检查权限的装饰器 def wrapper(*args,**kwargs): print("---start check perms",args[0]) if not perm_check(*args,**kwargs): return render(args[0],crm/403.html)  # args[0],对应的就是request return func(*args,**kwargs) #print("---done check perms") return wrapper

 





以上是关于权限管理的主要内容,如果未能解决你的问题,请参考以下文章

JS+JavaBean判断管理员增删改的操作权限

Android 逆向Linux 文件权限 ( Linux 权限简介 | 系统权限 | 用户权限 | 匿名用户权限 | 读 | 写 | 执行 | 更改组 | 更改用户 | 粘滞 )(代码片段

ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统

gitlab 可以进行版本和权限控制,bug管理吗?供部门内部使用,而且是Linux和Windows平台都能使用

使用 Git 来管理 Xcode 中的代码片段

教程4 - 验证和权限