
Originally Posted by
NuttySkunk
Wow, fantastic reply, thank you so much for the time and effort put into it!
By "per page basis", sorry for the confusion, I simply mean 'when a page (any particular page) loads how is authentication handled' as presumably any site that handles authentication will do so on each page of the site and generally speaking (as far as I am aware at my level of knowledge) regardless of what authentication is required to access a page it is still processed the same way?
User is unknown,
User is a registered member,
User is a registered contributor/moderator/editor,
User is a registered administrator.
Page requires no authentication,
Page requires member status - get authentication information, if user is equal to or greater than a member continue,
Page requires contributor/moderator/editor status - get authentication information, if user is equal to or greater than a contributor/moderator/editor continue,
Page requires administrator status - get authentication information, if user is equal to an administrator continue.
Or has that confused things further? Thanks again, I didn't think I was going to get any replys hehe.
First, no problem.
Second, what you describe there is a basic authentication and permissions system.
To answer your question:
1. The authentication will happen globally, so exists on all pages. Whatever mechanism you use for authentication will process everywhere so you will have access to your usergroup information for each user on ever page. Then every page can allow access or prevent access (entirely or to particular features of that page) based on the permissions within the usergroup.
2. How you authenticate will be dependant upon your system, normally you'd have some kind of architecture where you have a bootstrap mechanism that runs from index.php (no separate root php files for each page), within that process you'll initialize a framework that handles authentication. Authentication will usually be a combination of session and cookie.
Typically a user accounts credentials will consist of something like:
- UserID (unique user id)
- Username
- Password (encrypted hash)
- Salt (a salt value based on something account specific, for example join date)
The salt is used as a key against the password hash (combined they create a new hash which is what you store for the password and use to authenticate).
You do this on every script load to authenticate. Also you can have a session hash key which is compared to validate the session for added security.
Now what you describe is a really basic system and one I would avoid, you should go granular.
With a granular system you don't handle permissions as "isAdmin", "isGuest", "isModerator" etc.
Instead you have a usergroup table with more specific permissions, something like this:
USERGROUP (table)
- usergroupid (INT) Primary Key
- grouptitle (VARCHAR) // Name of the usergroup
- isBanned (BOOL) // A flag for whether the group is "banned"
- isGuest (BOOL) // A flag for whether the group is for guests
- isMember (BOOL) // A flag for whether the group is for members
- isModerator (BOOL) // A flag for whether the group is for moderators
- isAdministrator (BOOL) // A flag for whether the group is for administrators
* Cutting away from the fields for a moment as there are more, but something has to be said *
Those isBanned, isGuest, isMember fields are not for permissions, they are for hierarchy (exception with Banned, if thats true you lockout). Basically they add an order to things. i.e. isAdministrator can perform allowed actions on any group. isModerator can perform allowed actions only on isMember, isGuest and isBanned (maybe isModerator depending on your architecture).
In other words, it creates the user level. But does not define what you can actually do, you should have permissions for this. Take a website with a forum and news articles and pages and a CMS. I'll keep it simple:
- viewPage (BOOL) // Can the usergroup view pages on the front site
- createPage (BOOL) // Can the usergroup create pages (CMS)
- editPage (BOOL) // Can the usergroup edit pages (CMS)
- deletePage (BOOL) // Can the usergroup deletePages (CMS)
- viewArticle (BOOL) // Can the usergroup view articles
- createArticle (BOOL) // Can the usergroup create articles
- editArticle (BOOL) // Can the usergroup edit articles
- deleteArticle (BOOL) // Can the usergroup delete articles
- commentArticle (BOOL) // can the usergroup comment on articles
- viewForum (BOOL) // Can the usergroup view forums
- postThread (BOOL) // Can the usergroup post threads
- postReply (BOOL) // Can the usergroup post replies
- editPost (BOOL) // Can the usergroup edit posts (moderation)
- editOwnPost (BOOL) // Can the usergroup edit their own posts
- deletePost (BOOL) // Can the usergroup delete posts (moderation)
- deleteOwnPost (BOOL) // Can the usergroup delete their own posts
- closeThread (BOOL) // Can the usergroup close threads
- openThread (BOOL) // Can the usergroup open their own threads
* These are all Boolean, true/false fields. You can have other stuff like, if you have a private messenger.
- pmQuota (INT) // Maximum messages the user can have in their inbox
Then the usergroups themselves:
Banned (no permissions, is banned type)
Guest (is guest type, has read type fields to true, others to false)
Member (is member type, set to true/false for what you want members to do)
Moderator (is moderator type, set to true/false for what you want mods to do)
Administrator (is admin type, set to true/false for what you want admins to do)
Thats granular, your permission are details (like grains for each feature). It gives more control over permissions and allows you to flex your site/system more and makes things less "error prone".
You can then have levels to your permissions (different layers). Take a forum, posting rights. Most of your forums are open to thread creation and reply. But you have an announcements forum, which only moderators and admins can create threads, members can reply.
table:
FORUMPERMISSION
- permissionid (a unique id for the permission layer)
- forumid (the forum this layer is for)
- usergroupid (the usergroup this layer is for)
- viewForum (BOOL) // Can the usergroup view forums
- postThread (BOOL) // Can the usergroup post threads
- postReply (BOOL) // Can the usergroup post replies
- editPost (BOOL) // Can the usergroup edit posts (moderation)
- editOwnPost (BOOL) // Can the usergroup edit their own posts
- deletePost (BOOL) // Can the usergroup delete posts (moderation)
- deleteOwnPost (BOOL) // Can the usergroup delete their own posts
- closeThread (BOOL) // Can the usergroup close threads
- openThread (BOOL) // Can the usergroup open their own threads
If a record exists here for a specific forum, this set overrides the usergroups permissions, if a record doesnt exist (for the forum AND usergroup - joined key) then the usergroups permissions are used.
So for the announcements forum (forumid 1) you'd have a record for member which disabled thread posting and enabled replies and view. This then allows members to view and reply to threads but not create them.
Thats adding layers to granularity.
Then in your scripts you would check the permissions themselves.
i.e. on viewing a thread:
PHP Code:
if($permission['viewThread'] == false)
{
$errorManager->printNoPermission(); // Prints you do not have permission to view this thread
}
You should at least have granularity (in my opinion of course). Having built sites with granularity for so long, I cannot imagine going back.
Is this the kind of thing your wanting to know/get feedback on?
Bookmarks