/
UserIdentity.php
161 lines (149 loc) · 5.58 KB
/
UserIdentity.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/*
* LimeSurvey
* Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
*/
class UserIdentity extends CUserIdentity
{
protected $id;
protected $user;
protected $sOneTimePassword;
/**
* Checks whether this user has correctly entered password or not
*
* @access public
* @return bool
*/
public function authenticate($sOneTimePassword='')
{
if (Yii::app()->getConfig("auth_webserver")==false)
{
$user = User::model()->findByAttributes(array('users_name' => $this->username));
if ($user !== null)
{
if (gettype($user->password)=='resource')
{
$sStoredPassword=stream_get_contents($user->password); // Postgres delivers bytea fields as streams :-o
}
else
{
$sStoredPassword=$user->password;
}
}
else
{
$this->errorCode = self::ERROR_USERNAME_INVALID;
return !$this->errorCode;
}
if ($sOneTimePassword!='' && Yii::app()->getConfig("use_one_time_passwords") && md5($sOneTimePassword)==$user->one_time_pw)
{
$user->one_time_pw='';
$user->save();
$this->id = $user->uid;
$this->user = $user;
$this->errorCode = self::ERROR_NONE;
}
elseif ($sStoredPassword !== hash('sha256', $this->password))
{
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}
else
{
$this->id = $user->uid;
$this->user = $user;
$this->errorCode = self::ERROR_NONE;
}
}
elseif(Yii::app()->getConfig("auth_webserver") === true && isset($_SERVER['PHP_AUTH_USER'])) // normal login through webserver authentication
{
$sUser=$_SERVER['PHP_AUTH_USER'];
$aUserMappings=Yii::app()->getConfig("auth_webserver_user_map");
if (isset($aUserMappings[$sUser])) $sUser= $aUserMappings[$sUser];
$oUser=User::model()->findByAttributes(array('users_name'=>$sUser));
if (is_null($oUser))
{
if (function_exists("hook_get_auth_webserver_profile"))
{
// If defined this function returns an array
// describing the defaukt profile for this user
$aUserProfile = hook_get_autouserprofile($sUser);
}
elseif (Yii::app()->getConfig("auth_webserver_autocreate_user"))
{
$aUserProfile=Yii::app()->getConfig("auth_webserver_autocreate_profile");
}
}
if (Yii::app()->getConfig("auth_webserver_autocreate_user") && isset($aUserProfile) && is_null($oUser))
{ // user doesn't exist but auto-create user is set
$oUser=new User;
$oUser->users_name=$sUser;
$oUser->password=hash('sha256', createPassword());
$oUser->full_name=$aUserProfile['full_name'];
$oUser->parent_id=1;
$oUser->lang=$aUserProfile['lang'];
$oUser->email=$aUserProfile['email'];
$oUser->create_survey=$aUserProfile['create_survey'];
$oUser->create_user=$aUserProfile['create_user'];
$oUser->delete_user=$aUserProfile['delete_user'];
$oUser->superadmin=$aUserProfile['superadmin'];
$oUser->configurator=$aUserProfile['configurator'];
$oUser->manage_template=$aUserProfile['manage_template'];
$oUser->manage_label=$aUserProfile['manage_label'];
if ($oUser->save())
{
$aTemplates=explode(",",$aUserProfile['templatelist']);
foreach ($arrayTemplates as $sTemplateName)
{
$oRecord=new Templates_rights;
$oRecord->uid = $oUser->uid;
$oRecord->folder = trim($sTemplateName);
$oRecord->use = 1;
$oRecord->save();
}
// read again user from newly created entry
$this->id = $oUser->uid;
$this->user = $oUser;
$this->errorCode = self::ERROR_NONE;
}
else
{
$this->errorCode = self::ERROR_USERNAME_INVALID;
}
}
}
else
{
$this->errorCode = self::ERROR_USERNAME_INVALID;
}
return !$this->errorCode;
}
/**
* Returns the current user's ID
*
* @access public
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Returns the active user's record
*
* @access public
* @return CActiveRecord
*/
public function getUser()
{
return $this->user;
}
}