Sunday, September 6, 2015

استخدام قيم بيئة المستخدم على مستوى قاعدة البيانات.

بسم الله و الحمد لله و الصلاة و السلام على رسول الله.

مثال على  قيم بيئة المستخدم هو المتغير :

 :APP_USER
v('APP_USER')


في ابكس  يمكن استرجاع قيم بيئة المستخدم من خلال SYS_CONTEXT على مستوى قاعدة البيانات.

مثال: 


غالبا  نضيف اربع أعمدة لاي جدول  و التي هي باللون الأخضر فيما يلي:

CREATE TABLE EMP (
ID NUMBER (5),
EMP_NAME VARCHAR2 (64),
CREATED_BY VARCHAR2 (32),
UPDATED_BY VARCHAR2 (32),
CREATED DATE ,
UPDATED DATE
)

غالبا ما نقوم تاستخدام القادح التالي لادخال القيم إلى الأعمدة الأربعة:

CREATE OR REPLACE  TRIGGER EMP_ADT_TRG BEFORE
  INSERT OR UPDATE ON EMP FOR EACH ROW 
BEGIN 
  
  IF INSERTING THEN 
   :NEW.CREATED_BY := NVL( V('APP_USER'),USER );
   :NEW.CREATED := SYSDATE;
  END IF;
  
  
  IF UPDATING THEN   
    :NEW.UPDATED_BY := NVL( V('APP_USER'),USER );
    :NEW.UPDATED := SYSDATE;
  END IF;
  
END;

النقطة الأولى: في الكود السابق هي استخدام NVL و الدي يستهلك موارد أكنر من COALESCE و عليه الكود يجب أن يكون:


  :NEW.UPDATED_BY := COALESCE( v('APP_USER'),USER );


النقطة الثانية: في الكود السابق هي استخدام  
                                                                                                                 v('APP_USER')

و التي لا يمكن استخدامها كقيمة افتراضية للعمود في الجدول.


CREATE TABLE EMP (
ID NUMBER (5),
EMP_NAME VARCHAR2 (64),
CREATED_BY VARCHAR2 (32) 
default COALESCE(SYS_CONTEXT('APEX$SESSION', 'app_user'), USER),
UPDATED_BY VARCHAR2 (32),
CREATED DATE
default sysdate ,
UPDATED DATE
)

CREATE OR REPLACE TRIGGER EMP_ADT_TRG BEFORE

  UPDATE ON EMP  FOR EACH ROW
BEGIN

  :NEW.UPDATED_BY := COALESCE(SYS_CONTEXT('APEX$SESSION', 'app_user'), USER);
  :NEW.UPDATED := SYSDATE;

END;




Friday, November 9, 2012

Smarter Form - Form validation: client side




CSS Rules:

1- for text field:
input:required:valid {
  border-color: #88a !important ;
  -webkit-box-shadow: 0 0 3px rgba(0, 0, 255, .5) !important;
-moz-box-shadow:  0 0 3px rgba(0, 0, 255, .5) !important;
box-shadow:  0 0 3px rgba(0, 0, 255, .5) !important;
}
input:required:invalid, input:focus:invalid {
    border-color: #e88 !important;
    -webkit-box-shadow: 0 0 5px rgba(255, 0, 0, .8) !important;
-moz-box-shadow: 0 0 5px rgba(255, 0, 0, .8) !important;
box-shadow: 0 0 5px rgba(255, 0, 0, .8) !important;
}


2- for list of value field (If you wish):

select:required:valid {
  border-color: #88a !important ;
  -webkit-box-shadow: 0 0 5px rgba(0, 0, 255, .5) !important;
-moz-box-shadow: 0 0 5px rgba(0, 0, 255, .5) !important;
box-shadow: 0 0 5px rgba(0, 0, 255, .5) !important;
}
select:required:invalid, select:focus:invalid {
    border-color: #e88 !important;
    -webkit-box-shadow: 0 0 10px rgba(255, 0, 0, .8) !important;
-moz-box-shadow: 0 0 5px rgba(255, 0, 0, .8) !important;
box-shadow: 0 0 5px rgba(255, 0, 0, .8) !important;
    
}

Regular Expression:


^((\s*[a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}\s*[,;:]){1,100}?)?(\s*[a-zA-Z0-9\._%-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4})*$

pattern="........*"

Bonus: how to correct user entry:

You create a process - After submit - and you use something similar to this:

:P19_MOBILE := regexp_replace(regexp_replace(':P19_MOBILE','\D'),'^0*') ;

References:

http://apex.oracle.com/pls/apex/f?p=38997:1:
http://www.dba-oracle.com/oow_getting_regular_with_regular_expressions.pdf
http://www.the-art-of-web.com/html/html5-form-validation/



Sunday, October 14, 2012

Apex 4.2 has just been released

Salam All,

Apex 4.2 has just been released.

http://apex.oracle.com/i/index.html

Main features:


تسطيع تطوير تطبيقات الهواتف الذكية بسرعة و سهولة

Provide HTML 5 items types and charts.


Packaged Applications
حيث تمثل نقطة البداية لك لتتعلم كيف يتم تطوير تطبيقات أبكس


And a lot more ...

Thursday, January 12, 2012

كيف تبدأ مع أوراكل أبكلكيشن Oracle APEX


Download & Install Oracle 11g XE:







Click To Download Oracle 11g XE

عند عملية التنصيب قد تواجه مشكلة و الحل هنا


connect /as sysdba

SET SERVEROUTPUT ON ;
DECLARE
l_cfgxml XMLTYPE;
l_value VARCHAR2(5) := 'true'; -- (true/false)
BEGIN
l_cfgxml := DBMS_XDB.cfg_get();

IF l_cfgxml.existsNode('/xdbconfig/sysconfig/protocolconfig/httpconfig/allow-repository-anonymous-access') = 0 THEN
dbms_output.put_line('NO access anonymous');
ELSE
dbms_output.put_line('YES access anonymous');
end if;
end ;

====if it is NO you have to add or update ============
SET SERVEROUTPUT ON
DECLARE
l_cfgxml XMLTYPE;
l_value VARCHAR2(5) := 'true'; -- (true/false)
BEGIN
l_cfgxml := DBMS_XDB.cfg_get();
IF l_cfgxml.existsNode('/xdbconfig/sysconfig/protocolconfig/httpconfig/allow-repository-anonymous-access') = 0 THEN
-- Add missing element.
SELECT insertChildXML
(
l_cfgxml,
'/xdbconfig/sysconfig/protocolconfig/httpconfig',
'allow-repository-anonymous-access',
XMLType('<allow-repository-anonymous-access xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd">' ||
l_value ||
'</allow-repository-anonymous-access>'),
'xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"'
)
INTO l_cfgxml
FROM dual;
DBMS_OUTPUT.put_line('Element inserted.');
ELSE
-- Update existing element.
SELECT updateXML
(DBMS_XDB.cfg_get(),
'/xdbconfig/sysconfig/protocolconfig/httpconfig/allow-repository-anonymous-access/text()',
l_value,
'xmlns="http://xmlns.oracle.com/xdb/xdbconfig.xsd"')
INTO l_cfgxml
FROM dual;
DBMS_OUTPUT.put_line('Element updated.');
END IF;
DBMS_XDB.cfg_update(l_cfgxml);
DBMS_XDB.cfg_refresh;
END;

commit;
=====================================
step 3

ALTER USER ANONYMOUS IDENTIFIED BY anonymous;
ALTER USER ANONYMOUS ACCOUNT UNLOCK;
ALTER USER XDB IDENTIFIED BY xdb;
ALTER USER XDB ACCOUNT UNLOCK;

step 4
check if there is DAD APEX
====
set serverout on;
declare
my_dad_list dbms_epg.VARCHAR2_TABLE;
begin
DBMS_EPG.GET_DAD_LIST (my_dad_list);
for i in 1..my_dad_list.count
loop
dbms_output.put_line(my_dad_list(i));
end loop;
end;
IT should return APEX

===========

if it is empty you should run
@apex_epg_config.sql /home/oracle <--- dir where new apex was unziped (APEX is in /home/oracle/apex)
It will also load images.

========
step 5
change password for ADMIN APEX user:
@apxchpwd.sql


Upgrade APEX to the latest version:

Click To Download The Latest Version Of APEX



To Install the latest version  ( Upgrade ) follow these steps:
1. Unzip the downloadloaded zip file:
  • Double click <filename>.zip in Windows Explorer
  • [Note: You should keep the directory tree where you unzip the files short and not under directories that contain spaces. 
    For example, within Windows unzip to C:\.]
2. Change your working directory to apex.


  • In Windows 7: Go to Start Menu, then Type CMD. Then type: 
  • CD/
  • cd apex
  • 3. Start SQL*Plus and connect to the Oracle XE database
  • {Command prompt} C:\apex> sqlplus /nolog
     SQL> CONNECT SYS as SYSDBA
     Enter Password: 
     SYS_Password
      
4. Install Application Express:
  • SQL> @apexins SYSAUX SYSAUX TEMP /i/
5. Log back into SQL*Plus (as above) and load images:
  • SQL> @apxldimg.sql APEX_HOME
  • [Note: APEX_HOME is the directory you specified when unzipping the file. For example, with Windows 'C:\'.]
6. Upgrade Application Express password:
  • SQL> @apxchpwd
    Enter password for Application Express ADMIN account.
7. In a Web browser, navigate to the Oracle Application Express Administration Services application:
  • http://localhost:8080/apex/apex_admin
  • In Username, enter ADMIN
  • In Password, enter your password. ( that you chose in step 6 )


Unlocking  the APEX_PUBLIC_USER Account

Go To Start Menu and type CMD.
SYSTEM_DRIVE:\ sqlplus /nolog
SQL> CONNECT SYS as SYSDBA
Enter password: SYS_password


Run the following statements:
ALTER USER APEX_PUBLIC_USER ACCOUNT UNLOCK
ALTER USER APEX_PUBLIC_USER IDENTIFIED BY new_password



Enable Network Services in Oracle Database 11g

You may need this for the following reasons:

  • Sending outbound mail in Oracle Application Express.
  • Using Web services in Oracle Application Express.
  • PDF/report printing.

Copy and Past the following code in SQL:

DECLARE
  ACL_PATH  VARCHAR2(4000);
  ACL_ID    RAW(16);
BEGIN
  -- Look for the ACL currently assigned to '*' and give APEX_040100
  -- the "connect" privilege if APEX_040100 does not have the privilege yet.

  SELECT ACL INTO ACL_PATH FROM DBA_NETWORK_ACLS
   WHERE HOST = '*' AND LOWER_PORT IS NULL AND UPPER_PORT IS NULL;

  -- Before checking the privilege, ensure that the ACL is valid
  -- (for example, does not contain stale references to dropped users).
  -- If it does, the following exception will be raised:
  --
  -- ORA-44416: Invalid ACL: Unresolved principal 'APEX_040100'
  -- ORA-06512: at "XDB.DBMS_XDBZ", line ...
  --
  SELECT SYS_OP_R2O(extractValue(P.RES, '/Resource/XMLRef')) INTO ACL_ID
    FROM XDB.XDB$ACL A, PATH_VIEW P
   WHERE extractValue(P.RES, '/Resource/XMLRef') = REF(A) AND
         EQUALS_PATH(P.RES, ACL_PATH) = 1;

  DBMS_XDBZ.ValidateACL(ACL_ID);
   IF DBMS_NETWORK_ACL_ADMIN.CHECK_PRIVILEGE(ACL_PATH, 'APEX_040100',
     'connect') IS NULL THEN
      DBMS_NETWORK_ACL_ADMIN.ADD_PRIVILEGE(ACL_PATH,
     'APEX_040100', TRUE, 'connect');
  END IF;

EXCEPTION
  -- When no ACL has been assigned to '*'.
  WHEN NO_DATA_FOUND THEN
  DBMS_NETWORK_ACL_ADMIN.CREATE_ACL('power_users.xml',
    'ACL that lets power users to connect to everywhere',
    'APEX_040100', TRUE, 'connect');
  DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL('power_users.xml','*');
END;
/
COMMIT;



Download APEX listener:
http://www.oracle.com/technetwork/developer-tools/apex-listener/downloads/index.html



Download and Install Glass Fish Server:
http://glassfish.java.net/downloads/3.1.1-final.html

Download JDK
http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u2-download-1377129.html
Download SQL Developer.
http://www.oracle.com/technetwork/developer-tools/sql-developer/downloads/index.html



Friday, December 16, 2011

Oracle Apex - Auditing


بسم الله الرحمن الرحيم , الحمد لله و الصلاة و السلام على رسول الله

تعديل الجدول

ALTER TABLE demo_products ADD ( last_updated_by varchar2 (35), last_updated_on timestamp (4) with local time zone )

إنشاء القادح

CREATE OR REPLACE TRIGGER  "BI_product_info"
BEFORE INSERT OR UPDATE ON demo_product_info
FOR EACH ROW
BEGIN
  IF (inserting) THEN
    BEGIN
      :new.Last_updated_by := nvl(v('APP_USER'), 'user');
      :new.last_updated_on := systimestamp;
    END;
  ELSIF (updating) THEN
    BEGIN
      :NEW.Last_updated_by := nvl(v('APP_USER'), 'user');
      :NEW.last_updated_on := systimestamp;
   END;
  END IF;
END;


** Attributes
Region Attributes:   style=width:300px

** HTML Table Cell Attributes
style="color:#800517; font-weight: bold"

*** Do not forget to disable the tigger if you are going to upload data to your table:

ALTER TRIGGER bi_products DISABLE



APP_USER Syntax

Reference Type Syntax

  • Bind variable

:APP_USER

  • PL/SQL

V('APP_USER')

  • Substitution string

&APP_USER.

Consider the following examples:

From within an HTML region:Hello you are logged in as &APP_USER.
Using PL/SQL:

htp.p('Hello you are logged in as'||V('APP_USER'));
As a bind variable:

SELECT * FROM some_table WHERE user_id = :APP_USER


Sunday, October 9, 2011

Some Security Skills بعض النقاط حول الأمان


السلام عليكم و رحمة الله وبركاته, الحمد لله و الصلاة و السلام على رسول الله.
 اليوم إن شاء الله سنتكلم منح الصلاحيات للمتسخدمين لمشاهدة بيانات معينة::

  • تحديد البيانات المتاحة لكل مستخدم. فكل مستخدم يستطيع فقط أن يرى البيانات الخاصة به.
  • عرض بعض الأعمدة  و اخفاء البقية عن المستخدمين  الذين لا يملكون صلاحيات
  •  اخفاء  الحقول خلال فترة زمنية معينة أو جعل التطبيق متاح خلال ساعات معينة
  • جعل بعض الحقول للقراءة فقط بحيث لا يكمن تعديلها
-------------------------

1-
Select * from TABLE_NAME where upper (USER_NAME ) =  UPPER (:APP_USER) order by 1 desc 

إذا كنت أود أن أعطي صلاحيات لمستخدم اسمه اوراكل لكي يرى جيمع الصفوف

Select * from TABLE_NAME where upper (USER_NAME ) =  UPPER (:APP_USER) OR upper ( :APP_USER) = 'ORACLE' order by 1 desc 
-------------------------------------------

2-
click on edit page then Report Columns, ( Report Attributes tab). then go to Conditions.
SELECT * FROM DUAL WHERE upper (:app_user) not like 'ZIAD'
--------------------------------------------------
3-
click on edit item. then go to conditions.

TO_CHAR (SYSDATE, 'HH24') NOT BETWEEN '20' AND '23'

-----------------------------------------------------
يتبع إن شاء الله تعالى ....
4- We need to create Dynamic Action. (Show/Hide)

Go to Conditions. 
choose PL/SQL as Condition Type
 upper(:app_user) in ('X','Y','Z','F') and  :P2_empid is not null
في العادة نستخدم
:P2_ID
و في الاصدار الأخير من
APEX
نستخدم
:P2_RowID


الحمد لله و الصلاة و السلام على رسول الله