source: extensions/oAuth/include/test/examples/signin_signup/application/controllers/users.php @ 20293

Last change on this file since 20293 was 20293, checked in by mistic100, 11 years ago

first commit of oAuth plugin, still in developpement

File size: 4.2 KB
Line 
1<?php
2class users extends controller { 
3        function index()
4        {
5                if( isset( $_SESSION["user"] ) ){
6                        $this->redirect( "users/profile" );
7                }
8                else{
9                        $this->redirect( "users/login" );
10                }
11        }
12
13        function login()
14        {
15                $data = array();
16
17                // login form submitted?
18                if( count( $_POST ) ){
19                        // load user and authentication models
20                        $user = $this->loadModel( "user" );
21
22                        // get the user data from database
23                        $user_data = $user->find_by_email_and_password( $_POST["email"], $_POST["password"] );
24
25                        // user found?
26                        if( $user_data ){
27                                $_SESSION["user"] = $user_data["id"]; 
28
29                                $this->redirect( "users/profile" );
30                        }
31
32                        $data["error_message"] = '<b style="color:red">Bad Email or password! Try again.</b>';
33                }
34
35                // load login view
36                $this->loadView( "users/login", $data );
37        }
38
39        function logout()
40        {
41                // every thing is within php sessions, just destory it
42                $_SESSION = array(); 
43                session_destroy();
44
45                // go back home
46                $this->redirect( "users/login" );
47        }
48
49        function register()
50        {
51                $data = array();
52
53                // load user model
54                $user = $this->loadModel( "user" ); 
55
56                // registration form submitted?
57                if( count( $_POST ) ){
58                        $email      = $_POST["email"];
59                        $password   = $_POST["password"];
60                        $first_name = $_POST["first_name"];
61                        $last_name  = $_POST["last_name"];
62
63                        if( ! $email || ! $password ){
64                                $data["error_message"] = '<br /><b style="color:red">Your email and a password are required!</b>';
65                        }
66                        else{
67                                // check if email is in use?
68                                $user_info = $user->find_by_email( $email );
69
70                                // if email used on users table, we display an error
71                                if( $user_info ){
72                                        $data["error_message"] = '<br /><b style="color:red">Email alredy in use with another account!</b>';
73                                }
74                                else{
75                                        // create new user
76                                        $new_user_id = $user->create( $email, $password, $first_name, $last_name );
77
78                                        // set user connected
79                                        $_SESSION["user"] = $new_user_id; 
80 
81                                        $this->redirect( "users/profile" );
82                                }
83                        }
84                }
85
86                $this->loadView( "users/register", $data );
87        }
88
89        function complete_registration()
90        {
91                $data = array();
92
93                // load user model
94                $user = $this->loadModel( "user" ); 
95
96                // complete registration form submitted?
97                if( count( $_POST ) ){
98                        $email      = $_POST["email"];
99                        $password   = $_POST["password"];
100                        $first_name = $_POST["first_name"];
101                        $last_name  = $_POST["last_name"];
102
103                        if( ! $email || ! $password ){
104                                $data["error_message"] = '<br /><b style="color:red">Your email and a password are really important for us!</b>';
105                        }
106                        else{
107                                // check if email is in use?
108                                $user_info = $user->find_by_email( $email ); 
109
110                                // if email used on users table, we display an error
111                                if( $user_info && $user_info["id"] != $_SESSION["user"] ){
112                                        $data["error_message"] = '<br /><b style="color:red">Email already in use with another account!</b>';
113                                }
114                                else{
115                                        // update user profile
116                                        $user->update( $_SESSION["user"], $email, $password, $first_name, $last_name );
117
118                                        // here we go
119                                        $this->redirect( "users/profile" );
120                                }
121                        }
122                }
123
124                // get the user data from database
125                $user_data = $user->find_by_id( $_SESSION["user"] );
126
127                // load complete registration form view
128                $data["user_data"] = $user_data;
129                $this->loadView( "users/complete_registration", $data ); 
130        }
131
132        function profile()
133        {
134                // user connected?
135                if( ! isset( $_SESSION["user"] ) ){
136                        $this->redirect( "users/login" );
137                }
138
139                // load user and authentication models
140                $user = $this->loadModel( "user" );
141                $authentication = $this->loadModel( "authentication" );
142
143                // get the user data from database
144                $user_data = $user->find_by_id( $_SESSION["user"] );
145
146                // provider like twitter, linkedin, do not provide the user email
147                // in this case, we should ask them to complete their profile before continuing
148                if( ! $user_data["email"] ){
149                        $this->redirect( "users/complete_registration" );
150                }
151
152                // get the user authentication info from db, if any
153                $user_authentication = $authentication->find_by_user_id( $_SESSION["user"] );
154
155                // load profile view
156                $data = array( "user_data" => $user_data, "user_authentication" => $user_authentication );
157                $this->loadView( "users/profile", $data );
158        }
159}
Note: See TracBrowser for help on using the repository browser.