Jump to content

OO Admin Stuff


Manixat
Go to solution Solved by Jessica,

Recommended Posts

Hello freaks.

 

I've been trying to familiarize with OOP for some time now and I've come to the point where I actually start building stuff. So now I'm trying to figure out what is the proper way of detecting an admin user. I have, of course, my user class and my admin class, which extends ( or I believe inherits is the correct term ) the user class. So if there's some content on a certain page that only admins are supposed to see, how do I detect if the user is an admin properly? What I can easily do is have an if statement checking the user on the db and depending on the outcome either load the user class or the admin class but that doesn't quite seem right to me, trying to cut down procedural code to the minimum and all.. So what is the proper way of doing this?

Link to comment
Share on other sites

  • Solution

You should probably just have admin be a property of your user class, not a whole new class.

But if you want it that way you can use instanceOf, or have a function isAdmin() return true in admin and false in user

Link to comment
Share on other sites

I would agree with Jessica in this instance, but I would also like to point out a "proper way" for doing similar things.

 

You need what is called a "factory". This factory would be responsible for creating different kinds of "User" objects dependant on a certain criteria.

 

Without going into to much detail, the end call might look something like:

$user = UserFactory::get(100);
Where 100 is the user id as stored in the database.

 

The user factory is then responsible for creating whatever "User" type needs to be created and returning it.

Link to comment
Share on other sites

I think it depends on whether you want several additional fields to be stored for admin users, which are completely irrelevant for regular members. If this is true, you may subclass the user/member class with additional properties being neatly mapped to database fields. If it is just a status however, you are better off simply defining an additional property in your user/member class to label the specific user as an admin user. It totally depends on the application you have.

 

And you may want to consider Data Mapper enterprise pattern to do the trick of fetching a user/admin object with a supplied criteria. These are good examples, I believe its quite flexible this way. You may extend its functionality to handle multiple criteria, say both a username and a password. The mapper can be extended to handle more than just single selection statement, you can read Matt Zandstra's book for more reference.

 

$user = $userMapper->findByID(1);
$user2 = $userMapper->findByUsername("Admin");
$user3 = $userMapper->findByEmail("youremail@gmail.com");
$user4 = $userMapper->findByIP("127.0.0.1");


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.