Skip to content

Commit

Permalink
Only look up users by username
Browse files Browse the repository at this point in the history
Fixes #159
  • Loading branch information
inghamn committed Apr 30, 2018
1 parent 9a2c382 commit 5fc22b3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
5 changes: 4 additions & 1 deletion scripts/migrations/1.1-1.2/databaseChanges.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
alter table eventTypes add cifsType varchar(128);
delete from people where firstname='';

alter table events add foreign key (eventType_id) references eventTypes(id);
alter table eventTypes add cifsType varchar(128);
alter table people add notifications boolean;
alter table people modify email varchar(255) unique;
2 changes: 1 addition & 1 deletion scripts/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ create table people (
department_id int unsigned,
firstname varchar(128) not null,
lastname varchar(128) not null,
email varchar(255) not null,
email varchar(255) unique,
phone varchar(16),
username varchar(40) unique,
password varchar(40),
Expand Down
18 changes: 12 additions & 6 deletions src/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ public function index()
{
if (isset($_POST['username'])) {
try {
$person = new Person($_POST['username']);
if ($person->authenticate($_POST['password'])) {
$_SESSION['USER'] = $person;
$user = Person::findByUsername($_POST['username']);
if (!$user) { throw new \Exception(Person::ERROR_UNKNOWN_PERSON); }

if ($user->authenticate($_POST['password'])) {
$_SESSION['USER'] = $user;
header('Location: '.$this->return_url);
exit();
}
Expand Down Expand Up @@ -110,9 +112,13 @@ public function logout()
private function registerUser(string $username)
{
try {
$_SESSION['USER'] = new Person($username);
header("Location: {$this->return_url}");
exit();
$user = Person::findByUsername($username);
if ($user) {
$_SESSION['USER'] = $user;
header("Location: {$this->return_url}");
exit();
}
throw new \Exception(Person::ERROR_UNKNOWN_PERSON);
}
catch (\Exception $e) {
$_SESSION['errorMessages'][] = $e;
Expand Down
20 changes: 19 additions & 1 deletion src/Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ class Person extends ActiveRecord

protected $department;

const ERROR_UNKNOWN_PERSON = 'person/unknown';

/**
* Returns the matching Person object or null if not found
*
* @return Person
*/
public static function findByUsername(string $username)
{
$zend_db = Database::getConnection();
$sql = 'select * from people where username=?';

$result = $zend_db->createStatement($sql)->execute([$username]);
if (count($result)) {
return new Person($result->current());
}
}

/**
* Populates the object with data
*
Expand Down Expand Up @@ -48,7 +66,7 @@ public function __construct($id=null)
$this->exchangeArray($result->current());
}
else {
throw new \Exception('person/unknown');
throw new \Exception(self::ERROR_UNKNOWN_PERSON);
}
}
}
Expand Down

0 comments on commit 5fc22b3

Please sign in to comment.