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>Authentication and Authorization</h1>
4
 
5
<p>
6
Before we set off to implement the user pages, we need to do some work to enable <a href="http://www.pradosoft.com/demos/quickstart/index.php?page=Advanced.Auth">authentication and authorization</a>.
7
</p>
8
 
9
<p>
10
We add two new modules to the application configuration as follows:
11
</p>
12
 
13
<com:TTextHighlighter CssClass="source" Language="xml">
14
<modules>
15
    ...TDataSourceConfig and TActiveRecordConfig modules...
16
 
17
    <module id="auth"
18
      class="System.Security.TAuthManager"
19
      UserManager="users"
20
      LoginPage="users.LoginUser" />
21
 
22
    <module id="users"
23
      class="System.Security.TDbUserManager"
24
      UserClass="Application.BlogUser" />
25
</modules>
26
</com:TTextHighlighter>
27
 
28
<p>
29
The <a href="http://www.pradosoft.com/docs/classdoc/TAuthManager">TAuthManager</a> module manages the whole authentication and authorization workflow. It uses the <tt>users</tt> module as its user manager (see below). By specifying the <tt>LoginPage</tt> property, we inform the auth manager to redirect user's browser to the <tt>LoginUser</tt> page when an authorization fails. We will describe how to create <tt>LoginUser</tt> in the next subsection.
30
</p>
31
 
32
<p>
33
The <tt>user</tt> module is of class <a href="http://www.pradosoft.com/docs/classdoc/TDbUserManager">TDbUserManager</a> which is responsible to verify the validity of a user and keep basic user data in the PHP session. The <tt>UserClass</tt> property is initialized as <tt>Application.BlogUser</tt>, which indicates the user manager would look for a <tt>BlogUser</tt> class under the directory <tt>protected</tt> (remember the alias <tt>Application</tt> refers to the <tt>protected</tt> directory) and use it to keep user's session data.
34
</p>
35
 
36
<p>
37
As we will see in later sections, in controls and pages, we can use <tt>$this->User</tt> to obtain the <tt>BlogUser</tt> object which contains the information of the user currently accessing the system.
38
</p>
39
 
40
<p>
41
Below is the implementation detail of <tt>BlogUser</tt>. Notice <a href="http://www.pradosoft.com/demos/quickstart/index.php?page=Database.ActiveRecord">Active Record</a> is used to perform DB query. For example, we use <tt>UserRecord::finder()->findByPk($username)</tt> to look for the primary key specified by <tt>$username</tt> in the <tt>users</tt> table.
42
</p>
43
 
44
<com:TTextHighlighter CssClass="source" Language="php">
45
// Include TDbUserManager.php file which defines TDbUser
46
Prado::using('System.Security.TDbUserManager');
47
 
48
/**
49
 * BlogUser Class.
50
 * BlogUser represents the user data that needs to be kept in session.
51
 * Default implementation keeps username and role information.
52
 */
53
class BlogUser extends TDbUser
54
{
55
    /**
56
     * Creates a BlogUser object based on the specified username.
57
     * This method is required by TDbUser. It checks the database
58
     * to see if the specified username is there. If so, a BlogUser
59
     * object is created and initialized.
60
     * @param string the specified username
61
     * @return BlogUser the user object, null if username is invalid.
62
     */
63
    public function createUser($username)
64
    {
65
        // use UserRecord Active Record to look for the specified username
66
        $userRecord=UserRecord::finder()->findByPk($username);
67
        if($userRecord instanceof UserRecord) // if found
68
        {
69
            $user=new BlogUser($this->Manager);
70
            $user->Name=$username;  // set username
71
            $user->Roles=($userRecord->role==1?'admin':'user'); // set role
72
            $user->IsGuest=false;   // the user is not a guest
73
            return $user;
74
        }
75
        else
76
            return null;
77
    }
78
 
79
    /**
80
     * Checks if the specified (username, password) is valid.
81
     * This method is required by TDbUser.
82
     * @param string username
83
     * @param string password
84
     * @return boolean whether the username and password are valid.
85
     */
86
    public function validateUser($username,$password)
87
    {
88
        // use UserRecord Active Record to look for the (username, password) pair.
89
        return UserRecord::finder()->findBy_username_AND_password($username,$password)!==null;
90
    }
91
 
92
    /**
93
     * @return boolean whether this user is an administrator.
94
     */
95
    public function getIsAdmin()
96
    {
97
        return $this->isInRole('admin');
98
    }
99
}
100
</com:TTextHighlighter>
101
 
102
</com:TContent>