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>EditUser</tt> Page</h1>
4
 
5
<p>
6
The <tt>EditUser</tt> page is very similar to the <a href="?page=Day3.CreateNewUser">NewUser</a>. The main difference is that when <tt>EditUser</tt> is initially requested, the input fields should be initialized with existing user information. Another slight difference is that <tt>EditUser</tt> can also be accessed by normal users.
7
</p>
8
 
9
<p>
10
To determine which user account is to be editted, we use the following policy:
11
</p>
12
<ul>
13
<li>If the current user is an administrator, he can edit any user account by specifying the account's username in a GET variable named 'username'. For example, <tt>http://hostname/blog/index.php?page=users.EditUser&username=demo</tt>.</li>
14
<li>If the current user is an administrator and the URl does not contain 'username', the administrator himself's data is being updated.</li>
15
<li>If the current user is a normal user, he can only edit his own account information, and he cannot modify his role data.</li>
16
</ul>
17
 
18
<p>
19
We create two files <tt>protected/pages/users/EditUser.page</tt> and <tt>protected/pages/users/EditUser.php</tt> to save the page template and page class, respectively.
20
</p>
21
 
22
<h2>Creating Page Template</h2>
23
<p>
24
As you may have guessed, the page template <tt>EditUser</tt> is largely the same as that of <tt>NewUser</tt>. Besides the difference in page title and the caption of the submit button, there are three main differences.
25
</p>
26
<ul>
27
<li>The "username" text box is replaced by a <a href="http://www.pradosoft.com/demos/quickstart/?page=Controls.Label">TLabel</a> control because we do not allow modifying username;</li>
28
<li>The validator for the "password" input is removed. This is because if the user does not provide a password during editting, it means the user does not want to change the password.</li>
29
<li>The "role" input is surrounded with <tt>TControl</tt> whose visibility is toggled according to the role of the currently logged-in user. If the user is not an administrator, the "role" input will not be displayed because normal users are not allowed to modify their roles.</li>
30
</ul>
31
</p>
32
 
33
<com:TTextHighlighter CssClass="source" Language="prado">
34
&lt;%@ Title="My Blog - Edit User" %>
35
 
36
&lt;com:TContent ID="Main">
37
 
38
<h1>Edit User</h1>
39
 
40
<span>Username:</span>
41
&lt;com:TLabel ID="Username" />
42
 
43
<br/>
44
<span>Password:</span>
45
<br/>
46
&lt;com:TTextBox ID="Password" TextMode="Password" />
47
 
48
<br/>
49
<span>Re-type Password:</span>
50
&lt;com:TCompareValidator
51
    ControlToValidate="Password"
52
    ControlToCompare="Password2"
53
    ErrorMessage="Your password entries did not match."
54
    Display="Dynamic" />
55
<br/>
56
&lt;com:TTextBox ID="Password2" TextMode="Password" />
57
 
58
<br/>
59
<span>Email Address:</span>
60
&lt;com:TRequiredFieldValidator
61
    ControlToValidate="Email"
62
    ErrorMessage="Please provide your email address."
63
    Display="Dynamic" />
64
&lt;com:TEmailAddressValidator
65
    ControlToValidate="Email"
66
    ErrorMessage="You entered an invalid email address."
67
    Display="Dynamic" />
68
<br/>
69
&lt;com:TTextBox ID="Email" />
70
 
71
&lt;com:TControl Visible="&lt;%= $this->User->IsAdmin %>">
72
<br/>
73
<span>Role:</span>
74
<br/>
75
&lt;com:TDropDownList ID="Role">
76
    &lt;com:TListItem Text="Normal User" Value="0" />
77
    &lt;com:TListItem Text="Administrator" Value="1" />
78
&lt;/com:TDropDownList>
79
&lt;/com:TControl>
80
 
81
<br/>
82
<span>First Name:</span>
83
<br/>
84
&lt;com:TTextBox ID="FirstName" />
85
 
86
<br/>
87
<span>Last Name:</span>
88
<br/>
89
&lt;com:TTextBox ID="LastName" />
90
 
91
<br/>
92
&lt;com:TButton Text="Save" OnClick="saveButtonClicked" />
93
 
94
&lt;/com:TContent>
95
</com:TTextHighlighter>
96
 
97
 
98
<h2>Creating Page Class</h2>
99
 
100
<p>
101
Based on the above description and template, we need to write a page class that initializes the inputs with the existing user information. In addition, the page class also needs to implement the <tt>saveButtonClicked()</tt> method which is attached to the "save" button's <tt>OnClick</tt> event.
102
</p>
103
 
104
<com:TTextHighlighter CssClass="source" Language="php">
105
class EditUser extends TPage
106
{
107
    /**
108
     * Initializes the inputs with existing user data.
109
     * This method is invoked by the framework when the page is being initialized.
110
     * @param mixed event parameter
111
     */
112
    public function onInit($param)
113
    {
114
        parent::onInit($param);
115
        if(!$this->IsPostBack)  // if the page is initially requested
116
        {
117
            // Retrieves the existing user data. This is equivalent to:
118
            // $userRecord=$this->getUserRecord();
119
            $userRecord=$this->UserRecord;
120
 
121
            // Populates the input controls with the existing user data
122
            $this->Username->Text=$userRecord->username;
123
            $this->Email->Text=$userRecord->email;
124
            $this->Role->SelectedValue=$userRecord->role;
125
            $this->FirstName->Text=$userRecord->first_name;
126
            $this->LastName->Text=$userRecord->last_name;
127
        }
128
    }
129
 
130
    /**
131
     * Saves the user account if all inputs are valid.
132
     * This method responds to the OnClick event of the "save" button.
133
     * @param mixed event sender
134
     * @param mixed event parameter
135
     */
136
    public function saveButtonClicked($sender,$param)
137
    {
138
        if($this->IsValid)  // when all validations succeed
139
        {
140
            // Retrieves the existing user data. This is equivalent to:
141
            $userRecord=$this->UserRecord;
142
 
143
            // Fetches the input data
144
            $userRecord->username=$this->Username->Text;
145
            // update password when the input is not empty
146
            if(!empty($this->Password->Text))
147
                $userRecord->password=$this->Password->Text;
148
            $userRecord->email=$this->Email->Text;
149
            // update the role if the current user is an administrator
150
            if($this->User->IsAdmin)
151
                $userRecord->role=(int)$this->Role->SelectedValue;
152
            $userRecord->first_name=$this->FirstName->Text;
153
            $userRecord->last_name=$this->LastName->Text;
154
 
155
            // saves to the database via Active Record mechanism
156
            $userRecord->save();
157
 
158
            // redirects the browser to the homepage
159
			$this->Response->redirect($this->Service->DefaultPageUrl);
160
        }
161
    }
162
 
163
    /**
164
     * Returns the user data to be editted.
165
     * @return UserRecord the user data to be editted.
166
     * @throws THttpException if the user data is not found.
167
     */
168
    protected function getUserRecord()
169
    {
170
        // the user to be editted is the currently logged-in user
171
        $username=$this->User->Name;
172
        // if the 'username' GET var is not empty and the current user
173
        // is an administrator, we use the GET var value instead.
174
        if($this->User->IsAdmin && $this->Request['username']!==null)
175
            $username=$this->Request['username'];
176
 
177
        // use Active Record to look for the specified username
178
        $userRecord=UserRecord::finder()->findByPk($username);
179
        if(!($userRecord instanceof UserRecord))
180
            throw new THttpException(500,'Username is invalid.');
181
        return $userRecord;
182
    }
183
}
184
</com:TTextHighlighter>
185
 
186
<com:TipBox>
187
The <tt>onInit()</tt> method is invoked by PRADO during one of the <a href="http://www.pradosoft.com/demos/quickstart/?page=Fundamentals.Pages">page lifecycles</a>. Other commonly overriden lifecycle methods include <tt>onPreInit()</tt>, <tt>onLoad()</tt> and <tt>onPreRender()</tt>.
188
</com:TipBox>
189
 
190
<h2>Adding Permission Check</h2>
191
<p>
192
To make the <tt>EditUser</tt> page also accessible by authenticated users (<tt>users="@"</tt>), we need to adjust the page configuration file <tt>protected/pages/users/config.xml</tt> accordingly.
193
</p>
194
 
195
<com:TTextHighlighter CssClass="source" Language="xml">
196
<?xml version="1.0" encoding="utf-8"?>
197
<configuration>
198
  <authorization>
199
    <allow roles="admin"/>
200
    <allow users="@" pages="EditUser"/>
201
    <deny users="*"/>
202
  </authorization>
203
</configuration>
204
</com:TTextHighlighter>
205
 
206
 
207
<h2>Testing</h2>
208
<p>
209
To test the <tt>EditUser</tt> page, visit the URL <tt>http://hostname/blog/index.php?page=users.EditUser&username=demo</tt>. You may be required to login first if you have not done so. Try logging in with different accounts (e.g. admin/demo, demo/demo) and see how the page displays differently.
210
</p>
211
 
212
</com:TContent>