Python decorator for permission
Question: it is my first time to use a decorator and I want to show something like this

    
    
    import functools
    
    def checkUser(tag):
        def dec(f0):
            @functools.wraps(f0)
            def wrapper(userName, loggedinUser):
    
                if userName == loggedinUser:
                     return "You are allowed to view this page"
                else:
                     return "Error"
            return wrapper
        return dec
    
    @checkUser
    def test2(userName, loggedinUser):
        return userName, loggedinUser
    
    print test2()
    

and I am pretty sure it wouldn't work, this is just my idea. I just copied
this codes and edited. I am really new to this.


Answer: For the code you given, if you just want to check the user, there is no need
for you to use the decorator, why not just compare them directly? Since you
want to use decorator the check for permission. I think you may want to do
this way:

    
    
    import functools
    
    def check_user(username, password):
        def dec(f):
            @functools.wraps(f)
            def wrapper(login_user, login_password):
                f(login_user, login_password)
                if username == login_user and password == login_password:
                     return "You are allowed to view this page"
                else:
                     return "Check your password and username"
            return wrapper
        return dec
    
    
    @check_user('jhon', 'passwd')
    def test2(login_user, login_password):
        print "hello, {0}".format(login_user)
    
    
    print test2('jhon', 'foobar')
    print test2('jhon', 'passwd')
    

and the output should be:

    
    
    hello, jhon
    Check your password and username
    hello, jhon
    You are allowed to view this page
    

you can see that on the first time, input the wrong password will give us the
warning, but when we input the right info, it is showing us the success
message.

