Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="Main">
2
 
3
<h1>Creating <tt>LoginUser</tt> Page</h1>
4
 
5
<p>
6
The <tt>LoginUser</tt> page displays a login form and authenticates a user who tries to login. As described in <a href="?page=Day3.Auth">authentication and authorization</a>, the user's browser is automatically redirected to the <tt>LoginUser</tt> page when the user is attempting to access a privileged page, such as a user admin page.
7
</p>
8
 
9
<p>
10
The workflow of <tt>LoginUser</tt> is very similar to the <a href="?page=Day1.CreateContact">Contact</a> page:
11
</p>
12
<ol>
13
<li>When a user accesses the <tt>LoginUser</tt> page, a login form is displayed;</li>
14
<li>The user fills in the username and password and clicks on the "login" button;</li>
15
<li>The <tt>LoginUser</tt> receives the "login" event and triggers the authentication sequence;</li>
16
<li>If the user enters correct username and password, the system assigns him a valid identity and redirects his browser to the desired privileged page; If not, a "password invalid" message is displayed.
17
</ol>
18
 
19
<p>
20
We create two files <tt>protected/pages/users/LoginUser.page</tt> and <tt>protected/pages/users/LoginUser.php</tt> to save the page template and page class, respectively.
21
</p>
22
 
23
<h2>Creating Page Template</h2>
24
 
25
<p>
26
Below we show the template for <tt>LoginUser</tt>. As we see, the page mainly contains a text box for collecting username and a text box for password. The username input is required, which is ensured by the <tt>TRequiredFieldValidator</tt>. The correctness of the password input is ensured by the <a href="http://www.pradosoft.com/demos/quickstart/index.php?page=Controls.Validation">TCustomValidator</a> which invokes the page's <tt>validateUser()</tt> method when validation is performed. The page also has "login" button which invokes the page's <tt>loginButtonClicked()</tt> when it is clicked.
27
</p>
28
 
29
<com:TTextHighlighter CssClass="source" Language="prado">
30
&lt;%@ Title="My Blog - Login" %>
31
 
32
&lt;com:TContent ID="Main">
33
 
34
<h1>Login</h1>
35
 
36
<span>Username:</span>
37
&lt;com:TRequiredFieldValidator
38
    ControlToValidate="Username"
39
    ErrorMessage="Please provide your username."
40
    Display="Dynamic" />
41
<br/>
42
&lt;com:TTextBox ID="Username" />
43
 
44
<br/>
45
<span>Password:</span>
46
&lt;com:TCustomValidator
47
    ControlToValidate="Password"
48
    ErrorMessage="Your entered an invalid password."
49
    Display="Dynamic"
50
    OnServerValidate="validateUser" />
51
<br/>
52
&lt;com:TTextBox ID="Password" TextMode="Password" />
53
 
54
<br/>
55
&lt;com:TButton Text="Login" OnClick="loginButtonClicked" />
56
 
57
&lt;/com:TContent>
58
</com:TTextHighlighter>
59
 
60
<h2>Creating Page Class</h2>
61
 
62
<p>
63
Like the <a href="?page=Day1.CreateContact">Contact</a> page, the <tt>LoginUser</tt> page also needs a class file which mainly contains the implementation of event handlers attached in the page template. Here, we need to implement two methods: <tt>validateUser()</tt> and <tt>loginButtonClicked()</tt>. In <tt>validateUser()</tt>, we use the <a href="?page=Day3.Auth">auth manager</a> to verify if the username and password are valid. If valid, the auth manager will automatically create a user session with appropriate user identity information.
64
</p>
65
 
66
<com:TTextHighlighter CssClass="source" Language="php">
67
class LoginUser extends TPage
68
{
69
    /**
70
     * Validates whether the username and password are correct.
71
     * This method responds to the TCustomValidator's OnServerValidate event.
72
     * @param mixed event sender
73
     * @param mixed event parameter
74
     */
75
    public function validateUser($sender,$param)
76
    {
77
        $authManager=$this->Application->getModule('auth');
78
        if(!$authManager->login($this->Username->Text,$this->Password->Text))
79
            $param->IsValid=false;  // tell the validator that validation fails
80
    }
81
 
82
    /**
83
     * Redirects the user's browser to appropriate URL if login succeeds.
84
     * This method responds to the login button's OnClick event.
85
     * @param mixed event sender
86
     * @param mixed event parameter
87
     */
88
    public function loginButtonClicked($sender,$param)
89
    {
90
        if($this->Page->IsValid)  // all validations succeed
91
        {
92
            // obtain the URL of the privileged page that the user wanted to visit originally
93
            $url=$this->Application->getModule('auth')->ReturnUrl;
94
            if(empty($url))  // the user accesses the login page directly
95
                $url=$this->Service->DefaultPageUrl;
96
            $this->Response->redirect($url);
97
        }
98
    }
99
}
100
</com:TTextHighlighter>
101
 
102
 
103
<h2>Testing</h2>
104
 
105
<p>
106
So we have created the <tt>LoginUser</tt> page. We can test it by visiting the URL <tt>http://hostname/blog/index.php?page=users.LoginUser</tt>. Remember in the <a href="?page=Day2.CreateDB">Creating Database</a> subsection, we already created two user accounts (username/password): <tt>admin/demo</tt> and <tt>demo/demo</tt>. We can use them to test our login page.
107
</p>
108
 
109
<img src="<%~ output.gif %>" class="output"/>
110
 
111
<h2>Adding Login/Logout Links to Master</h2>
112
 
113
<p>
114
To provide a direct way for users to login and logout, we modify the <tt>MainLayout</tt> master control a bit. In particular, we add a "login" hyperlink which links to the <tt>LoginUser</tt> page. We also add a "logout" link button which logs out a user when it is clicked.
115
</p>
116
 
117
<p>
118
We modify the footer section of the <tt>MainLayout</tt>'s template as follows. The visibility of "login" and "logout" is determined according to user's status. If the user is not logged in yet, i.e., <tt>$this->User->IsGuest</tt> is true, the "login" link is visible while the "logout" link is not; and vice versa.
119
</p>
120
 
121
<com:TTextHighlighter CssClass="source" Language="prado">
122
<div id="footer">
123
&lt;com:THyperLink Text="Login"
124
    NavigateUrl="&lt;%= $this->Service->constructUrl('users.LoginUser') %>"
125
    Visible="&lt;%= $this->User->IsGuest %>" />
126
 
127
&lt;com:TLinkButton Text="Logout"
128
    OnClick="logoutButtonClicked"
129
    Visible="&lt;%= !$this->User->IsGuest %>"
130
	CausesValidation="false" />
131
 
132
<br/>
133
&lt;%= PRADO::poweredByPrado() %>
134
</div>
135
</com:TTextHighlighter>
136
 
137
<p>
138
Since the "logout" button attaches its <tt>OnClick</tt> event with a method called <tt>logoutButtonClicked()</tt>, we need to modify the class file of <tt>MainLayout</tt> as well.
139
</p>
140
 
141
<com:TTextHighlighter CssClass="source" Language="php">
142
class MainLayout extends TTemplateControl
143
{
144
    /**
145
     * Logs out a user.
146
     * This method responds to the "logout" button's OnClick event.
147
     * @param mixed event sender
148
     * @param mixed event parameter
149
     */
150
    public function logoutButtonClicked($sender,$param)
151
    {
152
        $this->Application->getModule('auth')->logout();
153
        $url=$this->Service->constructUrl($this->Service->DefaultPage);
154
        $this->Response->redirect($url);
155
    }
156
}
157
</com:TTextHighlighter>
158
 
159
<p>
160
Now if we visit any page of our blog system, we should see either a link at the bottom of the page. The link displays "Login" if we have not logged in yet and "Logout" if we have logged in. If we click on "Logout", the browser will be redirected to the homepage and "Login" is displayed meaning we have logged out.
161
</p>
162
 
163
</com:TContent>