Welcome Guest, Not a member yet? Register   Sign In
dynamically create form from database tables
#1

[eluser]Future Webs[/eluser]
been a while since i posted anything here but having a brain freeze so thought id use the collective brain for a kick start.

I have two tables in my database that I use to manage permissions for users. For example, can this user access the forums, can they contact other users, can they change their username etc.

Instead of setting up a table and giving every user a row in the permissions table i opted for a small table of permissions and a small table that links permissions to users. When i query the table to see if a user has permission to do something if nothing is returned i know they dont have permission

My permissions table has fields for

- perm_id (autoinc)
- perm_title
- perm_desc

My user_groups (mtm) table looks like

- id (autoinc)
- user_id (links to my users id)
- perm_id (links to the permissions perm_id)

Now, onto building the form based on the possible permissions available

My thinking was that I grab an array of all possible permissions and then loop checkboxes or drop menus set to (y/n) (on/off) etc inside my form fields.

I can also create another array with the users actual permissions to tell me if each permission in the loop is active or disabled and then match them up to the form.

when the form is submitted i can then unpick the post data, clear the previous users permissions and then save the new active permissions.

Im coming unstuck in how i build the form though. Assigning the description and the human readable title is easy enough but how would i set the actual field names.

You might say im going about this the wrong way and making life hard for myself and you might have a very simple solution. I guess its a bit of help with the concept and then also with implementing the field names im after.

either way im happy to hear any suggestions
#2

[eluser]xerobytez[/eluser]
In my auth systems I always use nodes. So like i would have users.forums.post to allow posting to forums or admins.users.delete to allow an admin to delete users. Each node should always be unique, so I replace the periods in the node with underscores and use them in the form with a checkbox array and set the names to something like this name="permissions[users_forums_post]". Then in the controller on a post I remove all records from the permissions table for the user being edited, loop through the array of posted permissions, perform validation, and replace the underscores with periods. and then insert a new record into the permissions table with the new node. Hope this gives you some ideas.




Theme © iAndrew 2016 - Forum software by © MyBB