I have been trying to implement a Role Class Model pattern to my website User access mechanism (written in PHP). I have a few doubts though. Here is a simplified version of relevant code:
class User
{
public $role;
public $uid;
public function setRole($role)
{
$this->role = $role;
}
}
// role classes responsible for restricted actions
class BaseRole {}
class AdminRole extends BaseRole
{
// do something adminish + log action into database (with User ID)
public function SomethingAdminish($admin_id) { }
}
$user = new User();
$user->setRole(new AdminRole());
// pass Admin ID (User ID) into method
$user->rola->SomethingAdminish($user->uid);
I see some weakness here:
- Passing any other $user->uid into "SomethingAdminish" method will log incorrect information in to my log system (wrong User ID)
If I decide to log other User information in the above method, essentially I would have to pass whole User object as an argument, like so:
$user->rola->SomethingAdminish($user);
I am probably missing something essential here. Could you guys shed some light on the subject, please?