How to assign data only to users who have a specific role? - mysql

If I have data which I only want to assign to users who have an admin role, how would I represent this in the db schema? i.e. I have a users table, roles table, user_roles table and this particular user is linked to an admin role. How would I then be able to specify information only for admin users?

EDIT4: So if I understand, you have the users table, role table and an association linking users to roles.
One idea would be to put the value in the role table, then basically you'll have multiple role for multiple admin levels.
Or you can create other fields in the association table, to parametrize the association, but that would impact all other association. (or ou can have : X is in group B with option1=1, option2=42, option3=NULL and treat options fields differently depending on the group)
Other idea, but I would have made it completely differently:
you treat groups as users, with a special field call isGroup [True/False]. Then you create the groupAssociation table which stores which user belongs to which group.
Example :
USER
--Id-- --IsGroup--
A False
B False
C True
D True
GROUPASSOCIATION
--uid-- --BelongsTo--
A C
B D
C D
Note that you can cascade saying that C is included in D.
Then you create a privilege table and associate groups or user to certain privileges.
PRIVILEGE
--Id-- --Name--
1 Access Area 1
2 Access Area 2
3 Modify user
4 Edit on StackOverflow
PRIVILEGEASSOCIATION
--uid-- --pid--
A 4
C 2
D 1
C 3
So user A would have all privileges (one direct, two from C group, one for D group since C is included in D) and B would have only one (from group D)
Would it be better in your case?
EDIT3: Given your last comment, this in not valid anymore
The GRANT command will help you sort out your problem.
You can see it right there : GRANT
As an example :
GRANT SELECT ON db2.invoice TO 'jeffrey'#'localhost';
You can do it for a column only, for all table, or all database as well
EDIT: Oh well, let me check for groups. I forgot that part.
EDIT2: I didn't see anything about group permission tuning in MySQL : this said it's not possible but it's quite old. But I came accros this other SO question and they are using PHP to manage the groups.

I think the OP is trying to set a field in the database where the user_role is equal to admin role. If this is the case then you would need to do something like:
UPDATE field FROM users LEFT JOIN user_role ON user.id = userrole.userid
WHERE userrole.role = 'adminrole'
Now this would depend entirely on your schema, could you please post so we can give you a more accurate SQL command?
EDIT: ok reading the question again just confuses me. Please post your exact schema.
EDIT2:
With your example, it would make sense to store the extra field in the users table, since, an address is related to a user. You would only want to set this field if the users_role is equal to admin_user. This way you would also help if you needed to expand in the future. If not, you could also define another table called admin_addresses. This would only be specified for admin users and could look like: Admin_address_table (userID,address).
To enforce this logic (either way) you could use e.g. a stored procedure. Pass the data to a stored procedure and it would update the address field if the user_role was equal to admin_user. Alternmatively you could use application logic when updating your data.
It is really up to you which way to go. You have a better idea of what may happen in the future and what exact information needs to be stored. Hope this helps.

Related

why some users do not appear in mdl_user_info_data in Moodle?

I am getting students added to course using a query but i noticed 2 of them do not appear( even if they are active and listed as participants) checking inside tables i noticed they do not appear in mdl_user_info_data. How can i prevent this? or whats the reason they dont were added to this table
This is my query:
SELECT u.id,u.username,u.firstname,u.lastname,u.email, b.data
FROM mdl_user u,mdl_role_assignments r, mdl_user_info_data b
WHERE u.id=r.userid
AND u.id=b.userid
AND r.roleid=5
AND r.contextid = 'somecontextId'
ORDER BY u.email ASC;
The table mdl_user_info_data holds the values for custom user profile fields (that are defined in mdl_user_info_field).
If a user's profile has not been edited since a particular custom user profile field has been created, then there will not be an associated mdl_user_info_data record for them.
Note that if there is more than one custom user field defined, there can be more than one mdl_user_info_data field - so your query could return more than one record per user.
You probably want to rewrite your query to LEFT JOIN with mdl_user_info_data. You probably also want to LEFT JOIN with mdl_user_info_field to identify which of the custom user profile fields it relates to.
Also note that your query makes a number of assumptions that may not always be true - if your query is running inside Moodle code, then you should use {user_info_data} instead of the 'mdl_' prefix, as that prefix can be changed. Hard-coding roleid 5 for 'student' can also fail on some sites (although it is usually the case).

Creating a Whatsapp like model in Rails Framework.

Background: I am new to Rails and I have gone through Michael Hartl Ruby on Rails tutorial.
I have read Agile Web Development with Rails 5.
So for practice I was trying to design WhatsApp like framework and got stuck while creating User and Group model.
Problem: I have tried different user and group model relationship and each one is failing for some reason.I have tried few paths so I will enlist them below.I don't need code, just a correct database relationship between User,Admin,Group,Participants.Also we should be able to add relationship through UI.
We have two tables User and Group.Admin belongs_to Group.Group has_many Admins. Group has_many partcipants. Participant belongs_to group.Participant is a new table having user_id and group_id.
We have three tables User,Group and Admin_Participant.Admin_Participant table contains admin_of column and participant_of column and one user_id column.admin_of and participant_of will contain group_ids.
Similar to first but we have a different table for admin which contains user_id , group_id, boolean table for is_admin.
Group is reference to User and admin column is boolean.Participant is added to User table and is also a boolean.
One more Thing As soon as a user creates group he is assigned to be admin of that group.Remember the restrictions of participants.
3 tables ( and their models ) should suffice.
User
has many participants
has many groups through participants
Group
attribute: created_by ( user id )
has many participants
has many users through participant
Participant
belongs to group
belongs to user
attribute admin
The above should work provided that an admin is also a participant.
If there are admins who are not participants, then a extra column for 'participating', or 'non-participant' is required.
There is no need for an admin table unless the admin has other attributes.
In case you haven't, it's good to go through the guide
So this might be too far down the rabbit hole but I see only 2 tables, User and Groups. A user can be a admin and/or participant. The relationship between user and group would probably be has_or_belongs_to_many as a user can belong to many groups and a group can have many users. Further, to accomplish the user/admin/participant distinction, I might even subclass it with STI(single table inheritance) instead of using booleans to determine if someone is an admin or not.
as you specified you need a relationship model not code.
i think i can help.
you might have to reshape your whole database, but if i were you, i would do it like ...
User - name , user_id , mob_no , groups (before someone need to log in , my app would send em the code through which they can verify that they are the owner of that mob no. //we would not discuss that in detail/// )
######## has_many: groups
group - name , users , admin(boolean) (i made a different column for admin, the users column stores the user_id of the user , when someone creates a group he automatically becomes the admin , so admin column becomes true for that user id.. and later that admin can make other admin as well...[if you have problem implementing this i would suggest opening up michael hartl's book and read how he made an example user an admin and that admin could delete other users , that will help])
########## belongs_to: user
i do not understand how you would implement the admin of a group to add users to group,
so in this case i would like to suggest the following -
$ if the user has followed another user he can add that user to the group...
$ or a facebook like system " where user gets users invite to the group ,and later its upon him to decide whether he want to join or not.
% pls comment and lemme know if this helps. and any other suggestion.

sql - normalize user table with it groups but both can login

Hi currently i have 3 tables:
users
|id|name|email|password|last_login|created_at|
user_groups
|user_id|group_id
groups
|group_id|name|email|password|last_login|created_at|
Group can login so it when i can view statistics for specific all it users, that's why i put email and password too.
the problem is users and groups got almost everything same. 1 group can contain many users.
Is there anyway to make this more normalize and user still have their specific groups?
I would just use the user authentication to detect whether the user belongs to any group or not. That way you don't need all these extra fields for the group.
If you only want one specific user to be able to look at the group statistics you could add an admin_id to the user_groups table (which would relate to a user id)

Mysql Many to Many relationship

I am trying to implement a simple many to many relationship between two tables.
User and Groups.
User
---------
user_id
user_name
Group
----------
group_id
group_name
UserGroup
----------
user_id
group_id
Lets say both the user and group table each has 1000 entries.
I have to create one admin user that belongs to all groups.
Should I create 1000 entries in the UserGroup table for the "admin" user?
Can I create a boolean column say "Applicable_to_all_groups" in User table that should be checked first before selecting from UserGroup table?
Any suggestion on doing this the correct way will be appreciated.
Well, I would say there's no "true" solution for that kind of cases.
Let's look on some pros / cons
Solution 1, all in UserGroup table
Pros
Requests to get allowed groups is easier to write (no OR clause)
Cons
You will have to add an entry in this table every time you add an entry in the group table.
Doable, of course, but boring, and error-prone.
If you want a new User "which can also be related to all groups", you'll have to rewrite all your procedures / triggers / whatever you use to have "up-to-date" UserGroup table to add this new thing.
Solution2, flag (= boolean column)
Pros
Avoid unnecessary entries in your db (well, minor point)
Always "up-to-date", without any additional work.
Easy to add a new User with "all groups" rights (just put the flag to true)
Cons
You'll have to add OR clauses when requesting for allowed groups (based on flag or on GroupUser
table)
A personal point of view
I would go for the flag solution...

Link a relation

I stuck in a situtaion and hope I will get a solution here.
Situation is:
A user when wants to join the club he/she has to provide their identification.Lets say a citizen card,passport,driving license etc.
If a user want to get extra services from a club he/she has to provide other documents too like Bank Report,House property etc..
My problem is How to relate these documents .If I have to see the complete doucments of a user I should easily see all the documents given by a user.
Please help me.
I'll assume you have a user table, with a column called id which is a unique identifier for a particular user. Then you just need a table called documents with a column called document_id and a column called user_id (this one links to the id column in the user table).
Then you just do a query like so:
SELECT my_column_names FROM user
LEFT OUTER JOIN documents ON documents.user_id = user.id
WHERE some_condition
You'll probably want to store some information about the documents in the documents table, as well as either storing the document itself as a BLOB or just storing a filepath to where the document is saved on your file system; but that's really not part of the question.