Grima  2018-08
Whispering into Alma's ear
Public Member Functions | Static Public Member Functions | List of all members
GrimaUser Class Reference

Interface to the GrimaDB database to check password and get institution's apikey. More...

Inheritance diagram for GrimaUser:
GrimaDB

Public Member Functions

  addToDB ()
 
  updateDB ()
 
  deleteFromDB ()
 
- Public Member Functions inherited from GrimaDB
  offsetExists ($offset)
 
  offsetGet ($offset)
 
  offsetSet ($offset, $value)
 
  offsetUnset ($offset)
 
  getIterator ()
 

Static Public Member Functions

static  FromArray ( $data)
 
static  LookupUser ($username, $institution='', $password=FALSE)
 
static  RenameUser ( $username, $institution, $newusername)
 
static  ResetPassword ( $username, $institution, $password)
 
static  GetCurrentUser ()
 
static  SetCurrentUser ( $username, $password, $institution='')
 
static  LogoutCurrentUser ()
 
- Static Public Member Functions inherited from GrimaDB
static  init ()
 
static  isEmpty ()
 
static  isStateless ()
 
static  getInstitutions ()
 

Additional Inherited Members

- Protected Member Functions inherited from GrimaDB
  getPasswordAlgorithm ()
 
- Static Protected Member Functions inherited from GrimaDB
static  getDb ()
 

Detailed Description

Interface to the GrimaDB database to check password and get institution's apikey.

Definition at line 4020 of file grima-lib.php.

Member Function Documentation

◆ addToDB()

GrimaUser::addToDB ( )

Definition at line 4132 of file grima-lib.php.

4132  {
4133  $db = $this->getDb();
4134  $query = $db->prepare( 'INSERT INTO users( username, password, institution, isAdmin ) VALUES (:username, :password, :institution, :isAdmin)' );
4135  if (!$query) {
4136  $errorCode = $db->errorCode();
4137  $errorInfo = $db->errorInfo();
4138  throw new Exception(
4139  "Could not even prepare to insert into user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4140  );
4141  }
4142  $success = $query->execute( array(
4143  'username' => $this['username'],
4144  'password' => password_hash( $this['password'], $this->getPasswordAlgorithm() ),
4145  'institution' => $this['institution'],
4146  'isAdmin' => $this['isAdmin'],
4147  ) );
4148  if (!$success) {
4149  $errorCode = $query->errorCode();
4150  $errorInfo = $query->errorInfo();
4151  throw new Exception(
4152  "Could not insert into user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4153  );
4154  }
4155  }
getPasswordAlgorithm()
Definition: grima-lib.php:3943
static getDb()
Definition: grima-lib.php:3919
static $db
Definition: grima-lib.php:3898

◆ deleteFromDB()

GrimaUser::deleteFromDB ( )

Definition at line 4182 of file grima-lib.php.

4182  {
4183  $db = $this->getDb();
4184  $query = $db->prepare( 'DELETE FROM users WHERE username=:username AND institution=:institution' );
4185  if (!$query) {
4186  $errorCode = $db->errorCode();
4187  $errorInfo = $db->errorInfo();
4188  throw new Exception(
4189  "Could not even prepare to delete from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4190  );
4191  }
4192  $success = $query->execute( array(
4193  'username' => $this['username'],
4194  'institution' => $this['institution'],
4195  ) );
4196  if (!$success) {
4197  $errorCode = $query->errorCode();
4198  $errorInfo = $query->errorInfo();
4199  throw new Exception(
4200  "Could not delete from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4201  );
4202  }
4203  }
static getDb()
Definition: grima-lib.php:3919
static $db
Definition: grima-lib.php:3898

◆ FromArray()

static GrimaUser::FromArray (   $data )
static

Definition at line 4021 of file grima-lib.php.

References $key.

Referenced by GetCurrentUser(), and LookupUser().

4021  {
4022  $user = new GrimaUser();
4023  foreach ( $data as $key => $val ) {
4024  $user[$key] = $val;
4025  }
4026  return $user;
4027  }
$key
Definition: encrypt.php:4
Interface to the GrimaDB database to check password and get institution's apikey. ...
Definition: grima-lib.php:4020

◆ GetCurrentUser()

static GrimaUser::GetCurrentUser ( )
static

Definition at line 4112 of file grima-lib.php.

References FromArray().

Referenced by GrimaFormField\__construct().

4112  {
4113  if (!isset($_SESSION)) return false;
4114  return GrimaUser::FromArray( $_SESSION );
4115  }
static FromArray( $data)
Definition: grima-lib.php:4021

◆ LogoutCurrentUser()

static GrimaUser::LogoutCurrentUser ( )
static

Definition at line 4127 of file grima-lib.php.

References $grima.

4127  {
4128  global $grima;
4129  $grima->session_destroy();
4130  }
$grima
Definition: grima-lib.php:4293

◆ LookupUser()

static GrimaUser::LookupUser (   $username,
  $institution = '',
  $password = FALSE 
)
static

Definition at line 4029 of file grima-lib.php.

References FromArray().

Referenced by SetCurrentUser().

4029  {
4030  $db = self::getDb();
4031  $query = $db->prepare(
4032  'SELECT * ' .
4033  'FROM institutions NATURAL JOIN users '.
4034  'WHERE institution=:institution '.
4035  'AND username=:username'
4036  );
4037  $success = $query->execute( array(
4038  'institution' => $institution,
4039  'username' => $username,
4040  ) );
4041  if ($success) {
4042  $row = $query->fetch(PDO::FETCH_ASSOC);
4043  if ($row===false) return false;
4044  $user = GrimaUser::FromArray( $row );
4045  if ( ($password !== FALSE) && ($user['password']!='') ) {
4046  if ( !password_verify( $password, $user['password'] ) ) return false;
4047  if ( password_needs_rehash( $user['password'], $user->getPasswordAlgorithm() ) ) {
4048  $user['password'] = $password;
4049  $user->updateDB();
4050  }
4051  }
4052  unset( $user['password'] );
4053  return $user;
4054  } else {
4055  $errorCode = $query->errorCode();
4056  $errorInfo = $query->errorInfo();
4057  throw new Exception(
4058  "Could not select from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4059  );
4060  }
4061  }
static FromArray( $data)
Definition: grima-lib.php:4021
static $db
Definition: grima-lib.php:3898

◆ RenameUser()

static GrimaUser::RenameUser (   $username,
  $institution,
  $newusername 
)
static

Definition at line 4063 of file grima-lib.php.

4063  {
4064  $db = self::getDb();
4065  $query = $db->prepare(
4066  'UPDATE users ' .
4067  'SET username=:newusername ' .
4068  'WHERE institution=:institution '.
4069  'AND username=:username'
4070  );
4071  $success = $query->execute( array(
4072  'institution' => $institution,
4073  'username' => $username,
4074  'newusername' => $newusername,
4075  ) );
4076  if ($success) {
4077  return true;
4078  } else {
4079  $errorCode = $query->errorCode();
4080  $errorInfo = $query->errorInfo();
4081  throw new Exception(
4082  "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4083  );
4084  }
4085  }
static $db
Definition: grima-lib.php:3898

◆ ResetPassword()

static GrimaUser::ResetPassword (   $username,
  $institution,
  $password 
)
static

Definition at line 4087 of file grima-lib.php.

4087  {
4088  $db = self::getDb();
4089  $query = $db->prepare(
4090  'UPDATE users ' .
4091  'SET password=:password ' .
4092  'WHERE institution=:institution '.
4093  'AND username=:username'
4094  );
4095  $passwordHash = password_hash( $password, self::getPasswordAlgorithm() );
4096  $success = $query->execute( array(
4097  'institution' => $institution,
4098  'username' => $username,
4099  'password' => $passwordHash,
4100  ) );
4101  if ($success) {
4102  return true;
4103  } else {
4104  $errorCode = $query->errorCode();
4105  $errorInfo = $query->errorInfo();
4106  throw new Exception(
4107  "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4108  );
4109  }
4110  }
static $db
Definition: grima-lib.php:3898

◆ SetCurrentUser()

static GrimaUser::SetCurrentUser (   $username,
  $password,
  $institution = '' 
)
static

Definition at line 4117 of file grima-lib.php.

References $grima, and LookupUser().

4117  {
4118  global $grima;
4119  $user = GrimaUser::LookupUser( $username, $institution, $password );
4120  if ( $user !== false ) {
4121  $grima->session_save($user);
4122  } else {
4123  return false;
4124  }
4125  }
$grima
Definition: grima-lib.php:4293
static LookupUser($username, $institution='', $password=FALSE)
Definition: grima-lib.php:4029

◆ updateDB()

GrimaUser::updateDB ( )

Definition at line 4157 of file grima-lib.php.

4157  {
4158  $db = $this->getDb();
4159  $query = $db->prepare( 'UPDATE users SET isAdmin=:isAdmin, password=:password WHERE username=:username AND institution=:institution' );
4160  if (!$query) {
4161  $errorCode = $db->errorCode();
4162  $errorInfo = $db->errorInfo();
4163  throw new Exception(
4164  "Could not even prepare to update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4165  );
4166  }
4167  $success = $query->execute( array(
4168  'username' => $this['username'],
4169  'password' => password_hash( $this['password'], $this->getPasswordAlgorithm() ),
4170  'institution' => $this['institution'],
4171  'isAdmin' => $this['isAdmin'],
4172  ) );
4173  if (!$success) {
4174  $errorCode = $query->errorCode();
4175  $errorInfo = $query->errorInfo();
4176  throw new Exception(
4177  "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4178  );
4179  }
4180  }
getPasswordAlgorithm()
Definition: grima-lib.php:3943
static getDb()
Definition: grima-lib.php:3919
static $db
Definition: grima-lib.php:3898

The documentation for this class was generated from the following file: