Authorization in a more purely OOP style...

Posted by noblethrasher on Stack Overflow See other posts from Stack Overflow or by noblethrasher
Published on 2010-05-01T02:03:30Z Indexed on 2010/05/01 2:07 UTC
Read the original article Hit count: 265

Filed under:
|
|
|
|

I've never seen this done but I had an idea of doing authorization in a more purely OO way. For each method that requires authorization we associate a delegate. During initialization of the class we wire up the delegates so that they point to the appropriate method (based on the user's rights). For example:

class User
{
    private deleteMemberDelegate deleteMember;

    public StatusMessage DeleteMember(Member member)
    {
        if(deleteMember != null)
        {
            deleteMember(member);
        }
    }

    //other methods defined similarly...

    User(string name, string password) //cstor.
    {
        //wire up delegates based on user's rights. 
        //Thus we handle authentication and authorization in the same method.
    }

}

This way the client code never has to explictly check whether or not a user is in a role, it just calls the method. Of course each method should return a status message so that we know if and why it failed.

Thoughts?

© Stack Overflow or respective owner

Related posts about authorization

Related posts about oop