Oracle APEX 5.0 新手教程(五) 登录控制

Custom Authentication in Oracle APEX
1-  Introduction
2-  SQL Script
3-  Declaring Application Items
4-  Custom Authentication
http://blog.csdn.net/sunansheng/article/details/50409012

1- Introduction

This document is based on:
  • Oracle APEX 5.0

2- SQL Script

Create USER_ACCOUNT table:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
create table USER_ACCOUNT
(
   USER_NAME VARCHAR2(30) not null ,
   PASSWORD  VARCHAR2(30) not null ,
   USER_TYPE VARCHAR2(10) not null ,
   ACTIVE    VARCHAR2(1) not null ,
   EMAIL     VARCHAR2(64) not null ,
   FULL_NAME VARCHAR2(64) not null
) ;
  
alter table USER_ACCOUNT
   add constraint USER_ACCOUNT_PK primary key (USER_NAME) ;
alter table USER_ACCOUNT
   add constraint USER_ACCOUNT_UK unique (EMAIL) ;
 
-----------------------------------
 
insert into user_account (USER_NAME, PASSWORD , USER_TYPE,
  ACTIVE, EMAIL, FULL_NAME)
values ( 'tom' , 'tom123' , 'admin' , 'Y' , '[email protected]' , 'Tom' );
 
insert into user_account (USER_NAME, PASSWORD , USER_TYPE,
ACTIVE, EMAIL, FULL_NAME)
values ( 'jerry' , 'jerry123' , 'user' , 'Y' , '[email protected]' , 'Jerry' );
 
insert into user_account (USER_NAME, PASSWORD , USER_TYPE,
ACTIVE, EMAIL, FULL_NAME)
values ( 'donald' , 'donald123' , 'guest' , 'N' , '[email protected]' , 'Donald' );
 
Commit ;
  • PKG_SECURITY
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Create Or Replace Package Pkg_Security Is
 
    Function Authenticate_User(p_User_Name Varchar2
                              ,p_Password  Varchar2) Return Boolean;
 
    -----
    Procedure Process_Login(p_User_Name Varchar2
                           ,p_Password  Varchar2
                           ,p_App_Id    Number);
 
End Pkg_Security;
/
Create Or Replace Package Body Pkg_Security Is
 
    Function Authenticate_User(p_User_Name Varchar2
                              ,p_Password  Varchar2) Return Boolean As
       v_Password User_Account. Password %Type;
       v_Active   User_Account.Active%Type;
       v_Email    User_Account.Email%Type;
    Begin
       If p_User_Name Is Null Or p_Password Is Null Then
   
          -- Write to Session, Notification must enter a username and password
          Apex_Util.Set_Session_State( 'LOGIN_MESSAGE'
                                     , 'Please enter Username and password.' );
          Return False ;
       End If;
       ----
       Begin
          Select u.Active
                ,u. Password
                ,u.Email
          Into   v_Active
                ,v_Password
                ,v_Email
          From   User_Account u
          Where  u.User_Name = p_User_Name;
       Exception
          When No_Data_Found Then
       
             -- Write to Session, User not found.
             Apex_Util.Set_Session_State( 'LOGIN_MESSAGE'
                                        , 'User not found' );
             Return False ;
       End ;
       If v_Password <> p_Password Then
     
          -- Write to Session, Password incorrect.
          Apex_Util.Set_Session_State( 'LOGIN_MESSAGE'
                                     , 'Password incorrect' );
          Return False ;
       End If;
       If v_Active <> 'Y' Then
          Apex_Util.Set_Session_State( 'LOGIN_MESSAGE'
                                     , 'User locked, please contact admin' );
          Return False ;
       End If;
       ---
       -- Write user information to Session.
       --
       Apex_Util.Set_Session_State( 'SESSION_USER_NAME'
                                  ,p_User_Name);
       Apex_Util.Set_Session_State( 'SESSION_EMAIL'
                                  ,v_Email);
       ---
       ---
       Return True ;
    End ;
 
    --------------------------------------
    Procedure Process_Login(p_User_Name Varchar2
                           ,p_Password  Varchar2
                           ,p_App_Id    Number) As
       v_Result Boolean := False ;
    Begin
       v_Result := Authenticate_User(p_User_Name
                                    ,p_Password);
       If v_Result = True Then
          -- Redirect to Page 1 (Home Page).
          Wwv_Flow_Custom_Auth_Std.Post_Login(p_User_Name -- p_User_Name
                                             ,p_Password -- p_Password
                                             ,v( 'APP_SESSION' ) -- p_Session_Id
                                             ,p_App_Id || ':1' -- p_Flow_page
                                              );
       Else
          -- Login Failure, redirect to page 101 (Login Page).
          Owa_Util.Redirect_Url( 'f?p=&APP_ID.:101:&SESSION.' );
       End If;
    End ;
 
End Pkg_Security;
/

3- Declaring Application Items

Click on the  "Shared Components", here allows you to declare the  "Application Items", it is the items will be used in your application.
Enter a name for the Application Item is  "LOG_MESSAGE", its value is  "LOGIN_MESSAGE" attribute of Session, you can set its value in PL/SQL:
?
1
2
Apex_Util.Set_Session_State( 'LOGIN_MESSAGE'
                                              , 'User not found' );
Similarly, you create 2 Application Items:
  • SESSION_USER_NAME
  • SESSION_EMAIL

4- Custom Authentication

Open LOGIN page in APEX Page designer:
Create new Region:
Change the properties for the newly created Region.
Set display conditions for this Region.
Next you have to modify code handling process when the user clicks the Submit button.
  • PL/SQL Code:
?
1
2
3
Pkg_Security.Process_Login(:P101_USERNAME,
                            :P101_PASSWORD,
                            :APP_ID);
Save and running your apps:

你可能感兴趣的:(Oracle,APEX,Oracle,基础,Tutorial教程,Oracle,APEX新手教程)