How to display different interactive grid queries, depending on user privileges - sql

I use Master-Detail page. I want to display whole table only for user who is an administrator. For the user who isn't the administrator, I want to display sql query which will be restrict MD view.
I tried to create another one master detail on same page which is visible only for users without Administrator role. And first MD is not visible for them.(I used Server-side Condition) Is there exist some other way to display different query, depending on user role.
I hope I explained the problem clearly. Thanks in advance

Have a look at the package APEX_ACL, you can use the related views in your where clause.
Example:admins see all rows, other users only see the row for KING
SELECT *
FROM emp
WHERE
-- user is admin ?
( EXISTS ( SELECT 1
FROM apex_appl_acl_user_roles
WHERE application_id = :APP_ID AND
user_name = :APP_USER AND
role_static_id = 'ADMINISTRATOR'
) ) OR
-- user is no admin
( ename = 'KING' );

Related

Query Salesforce users and their permission sets (SOQL)

i'm trying to run an SOQL query on salesforce to get all users and their permission set id.
it is important the list return will be by user not by permission sets, meaning if i have 1000 users i will get back 1000 records and for each record the user attributes like email etc + permission sets list of Id's assign to him
SELECT+id,PermissionSet.id+FROM+User i tried finding the relationship field name but i'm not so familiar wtih salesforce, please assist
https://developer.salesforce.com/docs/atlas.en-us.238.0.object_reference.meta/object_reference/sforce_api_erd_profile_permissions.htm
The table you're looking for is PermissionSetAssignment
Top-down:
select id, email,
(select permissionsetid
from permissionsetassignments
where permissionset.isownedbyprofile = false)
from user
or bottom-up
select assigneeid, assignee.email, permissionsetid
from permissionsetassignment
where permissionset.isownedbyprofile = false
order by assigneeid

How to know currently logged in username in Oracle SQL?

How to know currently logged in username in Oracle SQL, if we are dealing with multiple users within the same DB, it is little bit confusing to understand!
By using this query we can get currently logged in user
select user from dual;
There are many different ways.
Option 1
Use the V$SESSION view.
SELECT USERNAME FROM V$SESSION;
Option 2
This one gives all the detailed information because there are times that you have to locate locked sessions.
select
substr(a.spid,1,9) pid,
substr(b.sid,1,5) sid,
substr(b.serial#,1,5) ser#,
substr(b.machine,1,6) box,
substr(b.username,1,10) username,
-- b.server,
substr(b.osuser,1,8) os_user,
substr(b.program,1,30) program
from v$session b, v$process a
where
b.paddr = a.addr
and type='USER'
order by os_user,username;

Setting a column value for only one user depending on results

I'm currently playing around with SQL and trying to find the best way to accomplish this:
I currently have a user table that has a user_id, organisation_id, registered_datetime. There are
a number of users in this table with different organisations. There may be 3 different users in
1 organisation, or 1 in 1 organisation, etc.
I have added a new column called admin_user and I am trying to string up an SQL statement together
to update the admin user column. There can only be one admin user per organisation, and I want
the user who registered the earliest for that organisation to be the admin.
I could do this manually but it would take time if I had a lot of users. What would be the best
way to accomplish this?
EDIT:
So I have a number of users like this with the columns. The ones highlighted are the users that has registered the earliest. I want to be able to set those users as an admin user. The only admin user within their organisation and set the rest to 0. 1 (Admin) 0 (Not Admin)
This SQL query will mark users which registered_datetime are lowest in its organisation_id as admin.
UPDATE users SET admin_user = 1
WHERE user_id IN (
SELECT u.user_id FROM users u
WHERE u.registered_datetime IS NOT NULL AND NOT EXISTS(
SELECT 1 FROM users iu
WHERE iu.organisation_id = u.organisation_id AND iu.registered_datetime < u.registered_datetime
)
)
You might want to update all users to admin_user = 0 before this code, so all your users will have their values set.
One caveat here, if two users in one organisation were registered in exact same time, then both of them will be marked as administrators.
Update
I have added u.registered_datetime IS NOT NULL into the WHERE clause to filter out users with NULL in registered_datetime.
MSSQL
In MsSql server I usually solve this problem a in another way, by using ROW_NUMBER():
WITH base AS (
SELECT user_id, ROW_NUMBER() OVER ( PARTITION BY organisation_id ORDER BY registered_datetime ASC ) AS rn
FROM user
WHERE registered_datetime IS NOT NULL
)
UPDATE user SET is_admin = 1
WHERE user_id IN (
SELECT base.user_id FROM base WHERE rn = 1
)
This is too long for a comment.
You are describing three different tables:
Users
Organizations
UserOrganizations
The last has one row per user and per organization. This provides the mapping between the two. This can be called a "mapping" table, "junction" table, or "association" table.
How you implement one admin per organization depends on the database you are using.
You do not need the admin_user column. You need a column isadmin.
When a user is registered, if he is the first in the organization, then the isadmin column has the value 1, otherwise 0
Also, you can use the AAA query to find the administrator
SELECT `table`.`user_id`, MIN(`table`.`registered_datetime`) WHERE `organisation_id`=...

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

how can I do this select query

I have in my SQL Server, a table called FavoriteUsers with a list of users and a table called Users with all users.
In my ASPX page, I have a TextBox where my user can put the ID of an User to list in my GridView.
How it works
By default, I fill my GridView with all data of my FavoriteUsers table, and if the user of my system put some user code in the textbox field and click on my button to find these users, I'll reload the GridView with the FavoriteUsers and the users that he put on TextBox field that come from the table Users.
How can I do this SELECT query ?
select * from FavoriteUsers
UNION
select * from Users
Where userid = 'TextBox.text'
Of course you can't use TextBox.text in there. But you only needed the query so you get the point.
NOTE: The UNION will leave out any records from Users that already exist in FavoriteUsers. So you won't have duplicates.
Get all favorite users plus the user whose username matches that entered into the textbox:
SELECT Users.*
FROM Users
LEFT JOIN FavoriteUsers ON Users.UserID = FavoriteUsers.UserID
WHERE (FavoriteUsers.UserID IS NOT NULL) OR (Users.Username = #Username)