SQL Select aspnet_UsersInRoles - multiple roles - sql

Suppose I had a customer table and each customer can belong to several roles. I'm trying to populate an old gridview of customers but if a customer is a member of a role like "external" then I do not want that customer to appear...BUT that particular customer is also a member of a role called "editor" thus the customer is still included.
In other words, how can I, in SQL, say...if that customer is in that role, don't include him at all even if he is a member of other roles.

Related

SQL query to retrieve unique record based on role and email fields

I was trying to retrieve some records based on my requirement. As per the current table, a store can have any number of managers and/or cashiers with their respective email addresses. Now, I am trying to retrieve a new set of records which will have only one manager and/or one cashier per branch with their respective email address (there are no criteria to pick up specific manager cashier)
Current table:
BranchName Role Email
-------------------------------------------------
CA Cashier Cashier1#gmail.com
CA Cashier Cashier2#gmail.com
NY Manager Manager1#gmail.com
NY Manager Manager2#gmail.com
MASS Manager Manager#gmail.com
MASS Manager Massm#gmail.com
Expected output:
BranchName Role Email
------------------------------------------------
CA Cashier Cashier1#gmail.com
NY Manager Manager1#gmail.com
MASS Manager massm#gmail.com
The output is based on the requirement that, branch to Role is one to one mapping whereas Role to branch is one- many ( role and email forms a uniqueness for that particular branch).
I have tried using the below query but I am able to see the duplicate roles for a single branch.
select
BranchName, Email, Role
from
(select
BranchName, Email, Role,
ROW_NUMBER() OVER (PARTITION BY Role ORDER BY Email) AS "C"
from
Store
where
Role IN ('Manager', 'General manager', 'Cashier')
)
where
C = 1;
In my above query, while partitioning by role it only display single record with single branch instead of single role for all branches. It could be because of the condition C=1, if I remove this, I am getting all the records.
I hope, I have provided enough details. Kindly provide some inputs to fix this issue.
Thanks
If you don't have a preference/rule for choosing an email address given a branch name and role, then you just do a simple GROUP BY to get a result:
SELECT BranchName,
Role,
MIN(Email) AS Email
FROM Store
GROUP BY BranchName,
Role

Moqui:Can we create parent and child relation between two users

I just want to create one organization(Pharmacy) and under that organization need a different kinds of user like admin user, inventory user, billing user......etc
In your case, there are two dimensions of data control
by userGroup
by organization
So the solution would be:
create organization ['Phrmaacy1', 'Phrmaacy2']
create person ['Admin1', 'Admin2', 'Inventory1', 'Inventory2'], and userAccount for these persons
create userGroup ['Admin', 'Inventory']
create partyRelationship ['Admin1', 'Inventory1'] as member of ['Phrmaacy1'], and ['Admin2', 'Inventory2'] as member of ['Phrmaacy2']
create userGroupMember to put userAccount of person ['Admin1', 'Admin2'] in userGroup ['Admin'], userAccount of person ['inventory1', 'inventory2'] in userGroup ['Inventory']
Use both organization and userGroup the user belong to to filter out data, and use userGroup (userGroupPermissions, artifactAuthz) to control permission the user is allowed to perform.

Multi Table Query Access Comparing Data

If I have three tables, one called Person, one called Owner and the other called Tenant. All three have SSN as one of the fields. What I want to do is compare the SSN from Person (that's the whole list) to see which ones do not show up in either OWner or Tenant so I can see which people in the database have never owned a unit or leased a unit. Then i would like to be able to delete these people out of the person table.
Thanks
One easy way to do this is using not in:
select p.*
from persons as p
where p.ssn not in (select ssn from owner) and
p.ssn not in (select ssn from tenant);

Handling table with multiple child entities with Discriminators

I have a user table with different types of users. The type of the user is determined by user_type column in user table. I have a company table which has one to many relationship with User. I have different classes for different users like Guest, Admin (children of User class) each with a discriminator value.
My Company class has:
private Set<Guest> guests;
private Set<Admin> admins;
How can I write a single hql query to join company and user table to populate guest users into guest set and admin users into admin set?
Like select company left outer join fetch company.guests left outer join admin.guests. I cannot find a way to include user_type while making these joins.

SQL Insert based on role

I have a database with 3 tables:
Table 1 (Department) - This is a table with columns for departments and departmentID's
Table 2 (SecurityMap) - This is a table that maps rolenames to department ID's
Table 3 (customer info) - this is the info that is displayed to users based on their role memberships
I have all of the SELECT based on role functions working.
What I need is to figure out how to insert a specific value into the DepartmentID column within Table 3 by default, based on the users role membership. For instance, when someone is adding a new row to the database - in addition to the data they are supplying within the "add" form, I need a default value inserted into this column. If they are a member of the Marketing role, it should be a 1, if they are a member of the IT role, it should be a 2, etc...
Ideally, this would be done without any knowledge to the user that it is even happening. I would assume that I need to use an "Instead Of" trigger, but I have no idea how to proceed....
Shouldn't be too difficult:
In your app, keep track of the logged-in user and their role.
When your app saves the customer data, make sure it passes the database the role ID as well as the user-entered data (a stored procedure would be ideal here)
When the database processes the supplied data, it saves the role ID into the appropriate column.
this should work provided the rolename-column (or id) is unique in the securityMap-table otherwise the select could return more than one value, maybe you need to select the departmentId differently then.
insert into customer_info(otherdata, departmentId) values('data', (select departmentId from securityMap where rolename = 'userrole'))
Edit:
since you mentioned db_owner maybe this can help you (from http://www.sqlservercentral.com/Forums/Topic411310-338-1.aspx)
WITH CTE_Roles (role_principal_id)
AS
(
SELECT role_principal_id
FROM sys.database_role_members
WHERE member_principal_id = USER_ID()
UNION ALL
SELECT drm.role_principal_id
FROM sys.database_role_members drm
INNER JOIN CTE_Roles CR
ON drm.member_principal_id = CR.role_principal_id
)
SELECT USER_NAME(role_principal_id) RoleName
FROM CTE_Roles
ORDER BY RoleName;
you could join that with the SecurityMap-table to filter out roles like db_owner