Implemented picking users based on email on the checkout page
This commit is contained in:
parent
6cc74873b2
commit
5e21567204
@ -534,12 +534,22 @@
|
||||
list="user_suggest"
|
||||
autocomplete="off"
|
||||
placeholder="Användarnamn"
|
||||
value="¤user¤"
|
||||
required />
|
||||
value="¤user¤" />
|
||||
<button type="submit" >
|
||||
Välj
|
||||
</button>
|
||||
</div>
|
||||
<div>
|
||||
<label for="email">
|
||||
E-post:
|
||||
</label>
|
||||
<input type="text"
|
||||
name="email"
|
||||
id="email"
|
||||
autocomplete="off"
|
||||
placeholder="E-post"
|
||||
value="¤email¤" />
|
||||
</div>
|
||||
<div>
|
||||
<label for="displayname">
|
||||
Namn:
|
||||
|
@ -1,30 +1,68 @@
|
||||
<?php
|
||||
class CheckoutPage extends Page {
|
||||
private $userstr = '';
|
||||
private $emailstr = '';
|
||||
private $user = null;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
if(isset($_GET['user'])) {
|
||||
$this->userstr = trim(strtolower($_GET['user']));
|
||||
}
|
||||
if(isset($_GET['email'])) {
|
||||
$this->emailstr = trim(strtolower($_GET['email']));
|
||||
}
|
||||
try {
|
||||
$this->user = $this->user_init($this->userstr,
|
||||
$this->emailstr);
|
||||
} catch(Exception $e) {
|
||||
$this->error = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
protected function user_init($name, $email) {
|
||||
$nameuser = null;
|
||||
$emailuser = null;
|
||||
if($name) {
|
||||
try {
|
||||
$this->user = new User($this->userstr, 'name');
|
||||
$nameuser = new User($this->userstr, 'name');
|
||||
} catch(Exception $ue) {
|
||||
# The user wasn't found locally
|
||||
try {
|
||||
$ldap = new Ldap();
|
||||
$ldap->get_user($this->userstr);
|
||||
$this->user = User::create_user($this->userstr);
|
||||
$this->ldap->get_user($this->userstr);
|
||||
$nameuser = User::create_user($this->userstr);
|
||||
} catch(Exception $le) {
|
||||
$this->error = "Användarnamnet '";
|
||||
$this->error .= $this->userstr;
|
||||
$this->error .= "' kunde inte hittas.";
|
||||
$err = "Användarnamnet '$name' kunde inte hittas.";
|
||||
throw new Exception($err);
|
||||
}
|
||||
}
|
||||
}
|
||||
if($email) {
|
||||
try {
|
||||
# Lookup email directly in ldap since we don't store it
|
||||
$emailuser = new User($this->ldap->search_email($email),
|
||||
'name');
|
||||
} catch(Exception $ue) {
|
||||
$err = "E-postadressen '$emailuser' kunde inte hittas.";
|
||||
throw new Exception($err);
|
||||
}
|
||||
}
|
||||
if($nameuser && $emailuser) {
|
||||
if($nameuser != $emailuser) {
|
||||
$err = "Användarnamn och e-post matchar olika användare.";
|
||||
throw new Exception($err);
|
||||
}
|
||||
return $nameuser;
|
||||
}
|
||||
if($nameuser) {
|
||||
return $nameuser;
|
||||
}
|
||||
return $emailuser;
|
||||
}
|
||||
|
||||
protected function render_body() {
|
||||
$username = '';
|
||||
$email = '';
|
||||
$displayname = '';
|
||||
$notes = '';
|
||||
$loan_table = '';
|
||||
@ -33,6 +71,7 @@ class CheckoutPage extends Page {
|
||||
$disabled = 'disabled';
|
||||
if($this->user !== null) {
|
||||
$username = $this->user->get_name();
|
||||
$email = $this->user->get_email($this->ldap);
|
||||
$displayname = $this->user->get_displayname($this->ldap);
|
||||
$notes = $this->user->get_notes();
|
||||
$enddate = format_date(default_loan_end(time()));
|
||||
@ -45,7 +84,8 @@ class CheckoutPage extends Page {
|
||||
$subhead = replace(array('title' => 'Lånade artiklar'),
|
||||
$this->fragments['subtitle']);
|
||||
}
|
||||
print(replace(array('user' => $this->userstr,
|
||||
print(replace(array('user' => $username,
|
||||
'email' => $email,
|
||||
'displayname' => $displayname,
|
||||
'notes' => $notes,
|
||||
'end' => $enddate,
|
||||
|
@ -17,7 +17,8 @@ class Ldap {
|
||||
public function get_user($uid) {
|
||||
$data = $this->search("uid=$uid", 'cn', 'uid');
|
||||
if($data['count'] !== 1) {
|
||||
throw new Exception("LDAP search for '$uid' did not return exactly one result");
|
||||
$err = "LDAP search for '$uid' did not return exactly one result";
|
||||
throw new Exception($err);
|
||||
}
|
||||
return $data[0]['cn'][0];
|
||||
}
|
||||
@ -25,20 +26,20 @@ class Ldap {
|
||||
public function get_user_email($uid) {
|
||||
$data = $this->search("uid=$uid", 'mail', 'uid');
|
||||
if($data['count'] !== 1) {
|
||||
throw new Exception("LDAP search for '$uid' did not return exactly one result");
|
||||
$err = "LDAP search for '$uid' did not return exactly one result";
|
||||
throw new Exception($err);
|
||||
}
|
||||
return $data[0]['mail'][0];
|
||||
}
|
||||
|
||||
public function search_user($uid) {
|
||||
$data = $this->search("uid=$uid", 'cn', 'uid');
|
||||
public function search_email($email) {
|
||||
$data = $this->search("mail=$email", 'mail', 'uid');
|
||||
$out = array();
|
||||
foreach($data as $result) {
|
||||
if(isset($result['uid'])) {
|
||||
$out[$result['uid'][0]] = $result['cn'][0];
|
||||
}
|
||||
if($data['count'] !== 1) {
|
||||
$err = "LDAP search for '$email' did not return exactly one result.";
|
||||
throw new Exception($err);
|
||||
}
|
||||
return $out;
|
||||
return $data[0]['uid'][0];
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user