打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ORACLE-BASE - Schema Owners and Application Users

Schema Owners and Application Users

In the context of this article, the schema owner represents the Oracleuser that owns all your database objects, while application users areOracle users that need access to those schema objects.

Allowing applications to make direct connections to the schema owner isa bad idea because it gives those applications far to many privileges,which can easily result in damage to your data and the objectsthemselves. Instead, it is better to define application users and grantthose users the necessary privileges on the schema owners objects.

This article presents two methods for achieving this separation andhighlights their pros and cons. For simplicities sake I‘ve only definedtwo roles, but you can define as many roles as you wish, making thesecurity as granular as you need for each type of application user.

CURRENT_SCHEMA Approach

This method uses the CURRENT_SCHEMA session attribute to automatically point application users to the correct schema.

First, we create the schema owner and an application user.
CONN sys/password AS SYSDBA-- Remove existing users and roles with the same names.DROP USER schema_owner CASCADE;DROP USER app_user CASCADE;DROP ROLE schema_rw_role;DROP ROLE schema_ro_role;-- Schema owner.CREATE USER schema_owner IDENTIFIED BY passwordDEFAULT TABLESPACE usersTEMPORARY TABLESPACE tempQUOTA UNLIMITED ON users;GRANT CONNECT, CREATE TABLE TO schema_owner;-- Application user.CREATE USER app_user IDENTIFIED BY passwordDEFAULT TABLESPACE usersTEMPORARY TABLESPACE temp;GRANT CONNECT TO app_user;
Notice that the application user can connect, but does not have any tablespace quotas or privileges to create objects.

Next, we create some roles to allow read-write and read-only access.
CREATE ROLE schema_rw_role;CREATE ROLE schema_ro_role;
We want to give our application user read-write access to the schema objects, so we grant the relevant role.
GRANT schema_rw_role TO app_user;
We need to make sure the application user has its default schemapointing to the schema owner, so we create an AFTER LOGON trigger to dothis for us.
CREATE OR REPLACE TRIGGER app_user.after_logon_trgAFTER LOGON ON app_user.SCHEMABEGINDBMS_APPLICATION_INFO.set_module(USER, ‘Initialized‘);EXECUTE IMMEDIATE ‘ALTER SESSION SET current_schema=SCHEMA_OWNER‘;END;/
Now we are ready to create an object in the schema owner.
CONN schema_owner/passwordCREATE TABLE test_tab (id          NUMBER,description VARCHAR2(50),CONSTRAINT test_tab_pk PRIMARY KEY (id));GRANT SELECT ON test_tab TO schema_ro_role;GRANT SELECT, INSERT, UPDATE, DELETE ON test_tab TO schema_rw_role;
Notice how the privileges are granted to the relevant roles. Withoutthis, the objects would not be visible to the application user. We nowhave a functioning schema owner and application user.
SQL> CONN app_user/passwordConnected.SQL> DESC test_tabName                                                  Null?    Type----------------------------------------------------- -------- ------------------------------------ID                                                    NOT NULL NUMBERDESCRIPTION                                                    VARCHAR2(50)SQL>
This method is ideal where the application user is simply analternative entry point to the main schema, requiring no objects of itsown. It is clean and doesn‘t require management of thousands ofsynonyms. I don‘t find it very useful for developers who need to makecopies or modify schema objects during development.

Synonym Approach

This method relies on synonyms owned by the application user to point to the correct location of the schema objects.

First, we create the users in a similar way to the previous example.
CONN sys/password AS SYSDBA-- Remove existing users and roles with the same names.DROP USER schema_owner CASCADE;DROP USER app_user CASCADE;DROP ROLE schema_rw_role;DROP ROLE schema_ro_role;-- Schema owner.CREATE USER schema_owner IDENTIFIED BY passwordDEFAULT TABLESPACE usersTEMPORARY TABLESPACE tempQUOTA UNLIMITED ON users;GRANT CONNECT, CREATE TABLE TO schema_owner;-- Application user.CREATE USER app_user IDENTIFIED BY passwordDEFAULT TABLESPACE usersTEMPORARY TABLESPACE temp;GRANT CONNECT, CREATE SYNONYM TO app_user;
Once again, the application user can connect, but does not have anytablespace quotas. The difference here is that the application userdoes have the privilege to create synonyms.

Next, we create some roles to allow read-write and read-only access and grant the read-write role to the application user.
CREATE ROLE schema_rw_role;CREATE ROLE schema_ro_role;GRANT schema_rw_role TO app_user;
Now we are ready to create an object in the schema owner in the same way we did in the previous example.
CONN schema_owner/passwordCREATE TABLE test_tab (id          NUMBER,description VARCHAR2(50),CONSTRAINT test_tab_pk PRIMARY KEY (id));GRANT SELECT ON test_tab TO schema_ro_role;GRANT SELECT, INSERT, UPDATE, DELETE ON test_tab TO schema_rw_role;
If we now connect to the application user we are not able to see theobject without qualifying it with a schema name. We can either proceedin this fashion, or use a synonym to point to the correct object.
SQL> CONN app_user/passwordConnected.SQL> DESC test_tabERROR:ORA-04043: object test_tab does not existSQL> DESC schema_owner.test_tabName                                                  Null?    Type----------------------------------------------------- -------- ------------------------------------ID                                                    NOT NULL NUMBERDESCRIPTION                                                    VARCHAR2(50)SQL> CREATE SYNONYM test_tab FOR schema_owner.test_tab;Synonym created.SQL> DESC test_tabName                                                  Null?    Type----------------------------------------------------- -------- ------------------------------------ID                                                    NOT NULL NUMBERDESCRIPTION                                                    VARCHAR2(50)SQL>
I find this method rather cumbersome due to the sheer number ofsynonyms required, especially when there are a large number ofapplication users. Obviously, it is possible to use public synonyms,but this can be problematic when you have multiple application schemason a single instance. I only use this method when I have developers whoneed to create their own schema objects for testing.

Hope this helps. Regards Tim...
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
理解PostgreSQL数据库、模式、表、空间、用户间的关系
[Laskey99] Section 15.2. System Privileges
ORACLE 新增用户
oracle学习_表
oracle新建用户,授权,建表空间
Oracle 数据泵迁移用户创建 SQL语句
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服