3 require_once 'Pman.php';
9 * User class must provide the following features.
15 * active() -- is user active. // or set prior to checking..
16 * authUserName(n) - sets the value prior to a find(true)
17 * checkPassword($_REQUEST['password'])) {
19 * lang(val) - to set the language..
24 class Pman_Login extends Pman
27 var $masterTemplate = 'login.html';
29 function getAuth() // everyone allowed in here..
31 parent::getAuth(); // load company..
46 //DB_DataObject::DebugLevel(1);
47 if (!empty($_REQUEST['logout'])) {
48 $u = $this->getAuthUser();
52 $this->addEvent('LOGOUT');
53 $e = DB_DataObject::factory('Events');
55 session_regenerate_id(true);
60 $_SESSION['Pman_I18N'] = array();
65 if (!empty($_REQUEST['getAuthUser'])) {
66 //DB_Dataobject::debugLevel(5);
67 $this->sendAuthUserDetails();
72 // might be an idea to disable this?!?
73 if (!empty($_REQUEST['username'])) {
78 if (!empty($_REQUEST['switch'])) {
79 $this->switchUser($_REQUEST['switch']);
83 $this->jerr("INVALID REQUEST");
87 function sendAuthUserDetails()
89 // DB_DataObject::debugLevel(1);
90 $ff = HTML_FlexyFramework::get();
91 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
93 $u = DB_DataObject::factory($tbl);
95 $this->jok(array('id' => 0)); // not logged in..
99 $au = $u->getAuthUser();
101 // might occur on shared systems.
102 $ff= HTML_FlexyFramework::get();
104 if (!empty($ff->Pman['auth_comptype']) && $au->id > 0 &&
105 ($ff->Pman['auth_comptype'] != $au->company()->comptype)) {
107 $this->jerr("Login not permited to outside companies - please reload");
110 //$au = $u->getAuthUser();
112 $aur = $au->authUserArray();
114 /** -- these need modulizing somehow! **/
118 // basically calls Pman_MODULE_Login::sendAuthUserDetails($aur) on all the modules
119 //echo '<PRE>'; print_r($this->modules());
120 // technically each module should only add properties to an array named after that module..
122 foreach($this->modules() as $m) {
123 if (!file_exists($this->rootDir.'/Pman/'.$m.'/Login.php')) {
126 $cls = 'Pman_'.$m.'_Login';
127 require_once 'Pman/'.$m.'/Login.php';
130 $aur = $x->sendAuthUserDetails($aur);
135 //echo '<PRE>';print_r($aur);
144 function switchUser($id)
146 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
147 $u = DB_DataObject::factory($tbl);
149 $this->err("not logged in");
152 $au = $u->getAuthUser();
155 // first check they have perms to do this..
156 if (!$au|| ($au->company()->comptype != 'OWNER') || !$this->hasPerm('Core.Person', 'E')) {
157 $this->jerr("User switching not permitted");
161 $u = DB_DataObject::factory($tbl);
164 $this->jerr('Account disabled');
167 // we might need this later..
168 $this->addEvent("SWITCH USER", false, $au->name . ' TO ' . $u->name);
169 $this->jok("SWITCH");
177 //DB_DataObject::debugLevel(1);
178 if (!empty($_REQUEST['getAuthUser'])) {
179 $this->sendAuthUserDetails();
184 if (!empty($_REQUEST['passwordRequest'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
186 return $this->passwordRequest($_REQUEST['passwordRequest']);
190 if (!empty($_REQUEST['changePassword'])) {
191 return $this->changePassword($_REQUEST);
196 $ff = HTML_FlexyFramework::get();
197 $tbl = empty($ff->Pman['authTable']) ? 'Person' : $ff->Pman['authTable'];
200 $u = DB_DataObject::factory($tbl);
204 if (empty($_REQUEST['username'])) { //|| (strpos($_REQUEST['username'], '@') < 1)) {
205 $this->jerr('You typed the wrong Username or Password (0)');
209 $u->authUserName($_REQUEST['username']);
212 if ($u->count() > 1 || !$u->find(true)) {
213 $this->jerr('You typed the wrong Username or Password (1)');
218 $this->jerr('Account disabled');
221 // check if config allows non-owner passwords.
222 // auth_company = "OWNER" // auth_company = "CLIENT" or blank for all?
223 // perhaps it should support arrays..
224 $ff= HTML_FlexyFramework::get();
225 if (!empty($ff->Pman['auth_comptype']) && $ff->Pman['auth_comptype'] != $u->company()->comptype) {
226 die("Login not permited to outside companies"); // serious failure
231 if ($u->checkPassword($_REQUEST['password'])) {
233 // we might need this later..
234 $this->addEvent("LOGIN", false, session_id());
235 if (!empty($_REQUEST['lang'])) {
236 $u->lang($_REQUEST['lang']);
240 $this->sendAuthUserDetails();
247 $this->jerr('You typed the wrong Username or Password (2)'); // - " . htmlspecialchars(print_r($_POST,true))."'");
251 function passwordRequest($n)
253 $u = DB_DataObject::factory('Person');
254 //$u->company_id = $this->company->id;
256 $u->whereAdd('LENGTH(passwd) > 1');
259 if ($u->count() > 1 || !$u->find(true)) {
260 $this->jerr('invalid User (1)');
262 // got a avlid user..
263 if (!strlen($u->passwd)) {
264 $this->jerr('invalid User (2)');
266 // check to see if we have sent a request before..
268 if ($u->no_reset_sent > 3) {
269 $this->jerr('We have issued to many resets - please contact the Administrator');
271 $this->authFrom = time();
272 $this->authKey = $u->genPassKey($this->authFrom);
273 $this->authKey = md5($u->email . $this->authFrom . $u->passwd);
275 $ret = $u->sendTemplate('password_reset', $this);
276 if (is_object($ret)) {
277 $this->addEvent('SYSERR',false, $ret->getMessage());
278 $this->jerr($ret->getMessage());
280 $this->addEvent('PASSREQ',$u, $u->email);
282 $uu->no_reset_sent++;
288 function changePassword($r)
290 $au = $this->getAuthUser();
293 $au->setPassword($r['passwd1']);
297 // not logged in -> need to validate
298 if (empty($r['passwordReset'])) {
299 $this->jerr("invalid request");
301 // same code as reset pasword
303 $bits = explode('/', $r['passwordReset']);
306 $res= $this->resetPassword(@$bits[0],@$bits[1],@$bits[2]);
308 if ($res !== false) {
311 // key is correct.. let's change password...
313 $u = DB_DataObject::factory('Person');
315 //$u->company_id = $this->company->id;
316 $u->whereAdd('LENGTH(passwd) > 1');
318 if (!$u->get($bits[0])) {
319 $this->jerr("invalid id"); // should not happen!!!!
322 $u->setPassword($r['passwd1']);