Commit 2e851631 authored by Carson Greene's avatar Carson Greene

Laying initial groundwork for user auth

parent ec51a259
.vscode/sftp.json
composer.phar
vendor
\ No newline at end of file
{
"autoload": {
"psr-4": {"Knob\\": "lib/Knob"}
}
}
\ No newline at end of file
This diff is collapsed.
<?php
echo 1 + 2;
?>
\ No newline at end of file
<?php
namespace Knob;
/**
* A base table class for communicating with tables
*/
class Table {
/**
* Table constructor
* @param string $name The name of the table
* @param Site $site The global site object
*/
public function __construct(Site $site, $name) {
$this->site = $site;
$this->tableName = $name;
}
/**
* Get the database table name
* @return string The table name
*/
public function getTableName() {
return $this->tableName;
}
/**
* Diagnostic routine that substitutes into an SQL statement
* @param $query string The query with : or ? parameters
* @param $params array The arguments to substitute (what you pass to execute)
* @return string SQL statement with substituted values
*/
public function sub_sql($query, $params) {
$keys = array();
$values = array();
// build a regular expression for each parameter
foreach ($params as $key => $value) {
if (is_string($key)) {
$keys[] = '/:' . $key . '/';
} else {
$keys[] = '/[?]/';
}
if (is_numeric($value)) {
$values[] = intval($value);
} else {
$values[] = '"' . $value . '"';
}
}
$query = preg_replace($keys, $values, $query, 1, $count);
return $query;
}
/**
* Database connection function
* @return \PDO object that connects to the database
*/
public function pdo() {
return $this->site->pdo();
}
protected $site; //< The global site object
protected $tableName; //< The name of the table
}
\ No newline at end of file
<?php
namespace Knob;
class User {
/**
* User constructor
* @param array $row A row from the user table
*/
public function __construct($row) {
$this->memberNumber = $row['memberNum'];
$this->email = $row['email'];
$this->firstName = $row['firstName'];
$this->lastName = $row['lastName'];
$this->chapterId = $row['chapterId'];
$this->joined = \strtotime($row['joined']);
$this->role = $row['role'];
}
public function getMemberNumber() {
return $this->memberNumber;
}
public function getEmail() {
return $this->email;
}
public function getFullName() {
return $this->firstName + ' ' + $this->lastName;
}
public function getFirstName() {
return $this->firstName;
}
public function getLastName() {
return $this->lastName;
}
public function getChapter() {
// TODO: Return the chapter object for this user
}
public function getJoined() {
return $this->joined;
}
public function isPriveleged() {
return $this->role === self::ADMIN;
}
public function setEmail($email) {
$this->email = $email;
}
public function setName($last, $first) {
$this->name = $name;
}
public function setChapter($chapter) {
$this->chapterId = $chapter->getId();
}
public function setRole($role) {
$this->role = $role;
}
const ADMIN = 'A';
const BROTHER = 'B';
const SESSION_NAME = 'user';
private $memberNumber; //< The user's MySinfonia member number
private $email; //< The user's email address
private $firstName; //< The user's first name
private $lastName; //< The user's last name
private $chapterId; //< The id of the chapter the user is in (FK with chapter table)
private $joined; //< When the user joined
private $role; //< What priveleges the user has
}
?>
\ No newline at end of file
<?php
namespace Knob;
class Users extends Table {
/**
* Users constructor
* @param Site $site The global Site object
*/
public function __construct(Site $site) {
parent::__construct($site, 'user');
}
/**
* Get a user from the table
* @param int $memberNum The member number of the user to get
* @return User The user object
*/
public function get($memberNum) {
$sql = <<<SQL
select memberNum, email, firstName, lastName, chapterId, joined, role
from $this->tableName;
where memberNum = ?
SQL;
$pdo = $this->pdo();
$statement = $pdo->prepare($sql);
$result = $statement->execute([$memberNum]);
if ($result) {
$userRow = $statement->fetch(\PDO::FETCH_ASSOC);
if ($userRow !== null) {
return new User($userRow);
}
}
return null;
}
}
?>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment