vendredi 23 octobre 2015

Checking user role in pages

I have parent User class whose instance is returned when the user is authenticated. That User instance has role_id which indicates the user's role. There are three roles in the system; standarduser, trainer and gym (each has different table in database) and each has different functions and privileges.

There are some possibilities I can do;

1) I can write role checks in User's model class;

public function getName()
{
    if($user->role == 0)
    {
        $user->standarduser()->name;
    }
    else if($user->role == 1)
    {
        $user->gym()->name;
    }
}

public function getGymUsers()
{
    if($user->role == 2)
    {
        $user->gym()->users;
    }
    else
    {
        return null;
    }
}

2) I can write functions to each role class but in view (or controller), I have to do that control;

if($user->isStandarduser())
{
    echo $user->standarduser()->name;
}
. . .

if($user->isGym())
{
    echo $user->gym()->users;
}
else
{
    echo "";
}

3) Making specific views for each model so there will be no need to check roles. For example, if I'm in gym's page, I don't check if the object is a gym object because I know it's the gym's page.

I can't decide which way to go. If there are some better designs I can also try them.

Aucun commentaire:

Enregistrer un commentaire