Home » Developer & Programmer » Forms » Need help with button pressed trigger (Version 10.1.2.0.2)
Need help with button pressed trigger [message #538497] Fri, 06 January 2012 10:13 Go to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
Hi I have got a form with 3 buttons and when these are pressed determine who is logged in, what role they are and if they are a certain role go to another block, if they are not that role flag up a message. I think I need to create a function in the database along the lines of -
create function user_has_role (p_user in varchar2  default user, p_role  in varchar2) 
return boolean
as
 
select * from dba_role_privs where p_user = GRANTED_ROLE;
 
end 



Thankyou I am new to oracle Confused
Re: Need help with button pressed trigger [message #538504 is a reply to message #538497] Fri, 06 January 2012 10:41 Go to previous messageGo to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
few editions -
create function user_has_role(p_user in varchar2 default user, p_role in varchar2)
return boolean

as

select count(1) 
into v_count
from dba_role_privs
where grantee = p_user
and granted_role = p_role;
 
if v_count = 0
then
  return false;
else
  return true;
end if;
Re: Need help with button pressed trigger [message #538519 is a reply to message #538504] Fri, 06 January 2012 11:41 Go to previous messageGo to next message
cookiemonster
Messages: 13937
Registered: September 2008
Location: Rainy Manchester
Senior Member
You should use the all views instead of the dba ones, most users shouldn't have select on the dba views.
And use count(*) instead of count(1).
Re: Need help with button pressed trigger [message #538539 is a reply to message #538519] Fri, 06 January 2012 14:26 Go to previous messageGo to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
create or replace function user_has_role(p_user in varchar2 default user, p_role in varchar2)
return boolean as
  v_count  pls_integer;
begin
select count(*) 
into v_count
from dba_role_privs
where grantee = p_user
and granted_role = p_role;
if v_count = 0
then
  return false;
else
  return true;
end if;
end;

After this is created how would i determine by calling the function if it is the correct role or not, if it is go to block if it isnt display message?
Re: Need help with button pressed trigger [message #538541 is a reply to message #538539] Fri, 06 January 2012 15:12 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
if user_has_role('username','rolename') then
  -- user has the role, so do something
else
  -- user does not have role, so do something else
end if;


QED

However you still are using the dba taables, which should fail for every user except the DBA. And if it doesn't, then you have a security problem in your database.

[Updated on: Fri, 06 January 2012 15:14]

Report message to a moderator

Re: Need help with button pressed trigger [message #538586 is a reply to message #538541] Sat, 07 January 2012 08:30 Go to previous messageGo to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
I am still getting - Warning: Function created with compilation errors. Any ideas?
Re: Need help with button pressed trigger [message #538588 is a reply to message #538586] Sat, 07 January 2012 08:42 Go to previous messageGo to next message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Yes; after you get that message, execute SHOW ERRORS. It will tell you the reason. If you aren't capable of handling it, post the whole SQL*Plus session that creates a function, as well as the result of SHOW ERRORS. Post any additional information that might be relevant to the case (such as tables' description etc.).

You are expected to post something like the following copy/paste from my SQL*Plus session:
SQL> create or replace function fun_will_fail return number is
  2  begin
  3    select ename
  4      from emp
  5      where empno = 12345;
  6  end;
  7  /

Warning: Function created with compilation errors.

SQL> show errors
Errors for FUNCTION FUN_WILL_FAIL:

LINE/COL ERROR
-------- -----------------------------------------------------------------
3/3      PLS-00428: an INTO clause is expected in this SELECT statement
SQL>
Re: Need help with button pressed trigger [message #538591 is a reply to message #538588] Sat, 07 January 2012 08:51 Go to previous messageGo to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
Here you go -
SQL> create or replace function user_has_role(p_user in varchar2 default user, p_role in varchar2)
  2  return boolean as
  3    v_count  pls_integer;
  4  begin
  5  select count(*) 
  6  into v_count
  7  from dba_role_privs
  8  where grantee = p_user
  9  and granted_role = p_role;
 10  if v_count = 0
 11  then
 12    return false;
 13  else
 14    return true;
 15  end if;
 16  end;
 17  /

Warning: Function created with compilation errors.

SQL> show errors
Errors for FUNCTION USER_HAS_ROLE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
5/1      PL/SQL: SQL Statement ignored
7/6      PL/SQL: ORA-00942: table or view does not exist

[Updated on: Sat, 07 January 2012 08:54]

Report message to a moderator

Re: Need help with button pressed trigger [message #538593 is a reply to message #538591] Sat, 07 January 2012 09:05 Go to previous messageGo to next message
cookiemonster
Messages: 13937
Registered: September 2008
Location: Rainy Manchester
Senior Member
You don't have select privs on dba_role_privs, which is to be expected. As we've told you twice already - use all_role_privs instead.
Re: Need help with button pressed trigger [message #538595 is a reply to message #538593] Sat, 07 January 2012 09:10 Go to previous messageGo to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
same error with all_role_privs. I am logged in as dba trying to create this function therefore I have right to select from dba_role_privs. But yer it wont even let me select * from all_role_privs

SQL> select * from all_role_privs;
select * from all_role_privs
              *
ERROR at line 1:
ORA-00942: table or view does not exist
Re: Need help with button pressed trigger [message #538596 is a reply to message #538591] Sat, 07 January 2012 09:10 Go to previous messageGo to next message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
OK, you aren't allowed to select from DBA_ROLE_PRIVS. Acquire SELECT privilege from SYS:

SQL> connect scott/tiger
Connected.

SQL> select count(*) from dba_role_privs;
select count(*) from dba_role_privs
                     *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> connect sys/***** as sysdba
Connected.

SQL> grant select on dba_role_privs to scott;

Grant succeeded.

OK, try again:
SQL> connect scott/tiger
Connected.

SQL> select count(*) from dba_role_privs;

  COUNT(*)
----------
        68

SQL>

Cool, eh?
Re: Need help with button pressed trigger [message #538597 is a reply to message #538596] Sat, 07 January 2012 09:13 Go to previous messageGo to next message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
(There's no ALL_ROLE_PRIVS at all (so don't even bother to use it)).

SQL> show user
USER is "SYS"
SQL> select * from dictionary where table_name like '%ROLE%';

TABLE_NAME                     COMMENTS
------------------------------ ---------------------------------------------
DBA_ROLES                      All Roles which exist in the database
USER_ROLE_PRIVS                Roles granted to current user
DBA_ROLE_PRIVS                 Roles granted to users and roles
DBA_CONNECT_ROLE_GRANTEES      Information regarding which users are granted
                                CONNECT

ROLE_ROLE_PRIVS                Roles which are granted to roles
ROLE_SYS_PRIVS                 System privileges granted to roles
ROLE_TAB_PRIVS                 Table privileges granted to roles
SESSION_ROLES                  Roles which the user currently has enabled.

8 rows selected.

SQL>
Re: Need help with button pressed trigger [message #538598 is a reply to message #538597] Sat, 07 January 2012 09:18 Go to previous messageGo to next message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Quote:
I am logged in as dba trying to create this function therefore I have right to select from dba_role_privs.

What does "logged in as DBA" mean? DBA is a role. Privileges acquired via roles don't work on PL/SQL. You need to be granted SELECT on DBA_ROLE_PRIVS directly.

SYS owns that view, so SYS should grant privileges:
SQL> select owner, object_type
  2  from dba_objects
  3  where object_name = 'DBA_ROLE_PRIVS';

OWNER                          OBJECT_TYPE
------------------------------ -----------------
SYS                            VIEW
PUBLIC                         SYNONYM

SQL>
Re: Need help with button pressed trigger [message #538599 is a reply to message #538597] Sat, 07 January 2012 09:22 Go to previous messageGo to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
Ok ill grant the privilidges now

[Updated on: Sat, 07 January 2012 09:23]

Report message to a moderator

Re: Need help with button pressed trigger [message #538600 is a reply to message #538599] Sat, 07 January 2012 09:26 Go to previous messageGo to next message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Where did you take DBA_ROLES from? You were dealing with DBA_ROLE_PRIVS until now ...

Here you are, once again:
SQL> grant select on dba_role_privs to scott;

Grant succeeded.

SQL> connect scott/tiger
Connected.

SQL> create or replace function user_logged_in(p_user in varchar2 default user)
  2      return boolean as
  3        v_count  pls_integer;
  4      begin
  5      select count(*)
  6      into v_count
  7      from dba_role_privs
  8      where grantee = p_user;
  9      if v_count = 0
 10     then
 11       return false;
 12     else
 13       return true;
 14     end if;
 15     end;
 16     /

Function created.

SQL>

[Updated on: Sat, 07 January 2012 09:27]

Report message to a moderator

Re: Need help with button pressed trigger [message #538602 is a reply to message #538600] Sat, 07 January 2012 09:30 Go to previous messageGo to next message
winnitbaker
Messages: 20
Registered: January 2012
Junior Member
This is where I have a problem because I'm not the dba and only been given the dba privlidges and cant log in as SYS. DAM IT!!! Thanks for your efforts though guys. Aprreciate it!!
Re: Need help with button pressed trigger [message #538604 is a reply to message #538602] Sat, 07 January 2012 09:39 Go to previous message
Littlefoot
Messages: 21818
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
So contact your DBA and ask him to grant SELECT on DBA_ROLE_PRIVS directly to you.
Previous Topic: Cant execute query
Next Topic: books?
Goto Forum:
  


Current Time: Sun Sep 08 07:19:43 CDT 2024