/
registration.php
95 lines (80 loc) · 2.47 KB
/
registration.php
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
94
95
<?
Class LatticeAuth_Controller_Registration extends Controller_Layout {
protected $_actionsThatGetLayout = array(
'index',
'create',
'confirmed',
);
protected $errors = NULL;
protected $user_id = NULL;
public function action_index(){
$this->registrationView();
}
public function action_create(){
//run form validation
$user = $this->createUser($_POST['username'], $_POST['password'], $_POST['firstname'], $_POST['lastname'], $_POST['email']);
if($this->errors){
$this->registrationView();
} else {
$confirmation = new Confirmation('registration'
, $_POST['email'],
'registration',
'confirmed',
array($this->user_id)
);
$confirmation->send();
$view = new View('confirmationRequired');
$this->response->body($view->render());
}
}
public function action_confirmed($user_id){
$user = ORM::Factory('user',$user_id);
if(!$user->loaded()){
$this->response->body('Invalid Confirmation - User Does Not Exist');
return;
}
$user->status = 'ACTIVE';
$user->save();
$view = new View('registrationConfirmed');
$this->response->body($view->render());
}
protected function registrationView($errors=NULL){
$view = new View('registration');
$view->errors = $this->errors;
$this->response->body($view->render());
}
protected function createUser($username, $password, $firstname, $lastname, $email){
try {
$user = ORM::factory('user');
$user->status = 'INCOMPLETE';
$user->username = $username;
$user->password = $password;
// $user->firstname = $firstname;
//$user->lastname = $lastname;
$user->email = $email;
$user->save();
$this->user_id = $user->id;
}
/**/ catch (Exception $e){
$errors = array();
$modelErrors = $e->errors('validation');
if(isset($modelErrors['_external'])){
$modelErrors = array_values($modelErrors['_external']);
}
$this->user_id = NULL;
$this->errors = array_merge($errors, $modelErrors);
return false;
}
/**/
//add the login role
$user->add('roles', ORM::Factory('role', array('name'=>'login')));
if(is_array(Kohana::config('registration.default.roles'))){
foreach(Kohana::config('registration.default.roles') as $role){
$user->add('roles', ORM::Factory('role', array('name'=>$role)));
}
}
// //Other roles from config
$user->save();
return $user;
}
}