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 Public Member Functions inherited from GrimaDB
static init ()
 
static isEmpty ()
 

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 3233 of file grima-lib.php.

Member Function Documentation

◆ addToDB()

GrimaUser::addToDB ( )

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

References GrimaDB\$db, GrimaDB\getDb(), and GrimaDB\getPasswordAlgorithm().

3347  {
3348  $db = $this->getDb();
3349  $query = $db->prepare( 'INSERT INTO users( username, password, institution, isAdmin ) VALUES (:username, :password, :institution, :isAdmin)' );
3350  if (!$query) {
3351  $errorCode = $db->errorCode();
3352  $errorInfo = $db->errorInfo();
3353  throw new Exception(
3354  "Could not even prepare to insert into user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3355  );
3356  }
3357  $success = $query->execute( array(
3358  'username' => $this['username'],
3359  'password' => password_hash( $this['password'], $this->getPasswordAlgorithm() ),
3360  'institution' => $this['institution'],
3361  'isAdmin' => $this['isAdmin'],
3362  ) );
3363  if (!$success) {
3364  $errorCode = $query->errorCode();
3365  $errorInfo = $query->errorInfo();
3366  throw new Exception(
3367  "Could not insert into user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3368  );
3369  }
3370  }
getPasswordAlgorithm()
Definition: grima-lib.php:3166
static getDb()
Definition: grima-lib.php:3158
static $db
Definition: grima-lib.php:3170

◆ deleteFromDB()

GrimaUser::deleteFromDB ( )

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

References GrimaDB\$db, and GrimaDB\getDb().

3397  {
3398  $db = $this->getDb();
3399  $query = $db->prepare( 'DELETE FROM users WHERE username=:username AND institution=:institution' );
3400  if (!$query) {
3401  $errorCode = $db->errorCode();
3402  $errorInfo = $db->errorInfo();
3403  throw new Exception(
3404  "Could not even prepare to delete from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3405  );
3406  }
3407  $success = $query->execute( array(
3408  'username' => $this['username'],
3409  'institution' => $this['institution'],
3410  ) );
3411  if (!$success) {
3412  $errorCode = $query->errorCode();
3413  $errorInfo = $query->errorInfo();
3414  throw new Exception(
3415  "Could not delete from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3416  );
3417  }
3418  }
static getDb()
Definition: grima-lib.php:3158
static $db
Definition: grima-lib.php:3170

◆ FromArray()

static GrimaUser::FromArray (   $data)
static

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

Referenced by GetCurrentUser(), and LookupUser().

3234  {
3235  $user = new GrimaUser();
3236  foreach ( $data as $key => $val ) {
3237  $user[$key] = $val;
3238  }
3239  return $user;
3240  }
Interface to the GrimaDB database to check password and get institution's apikey. ...
Definition: grima-lib.php:3233

◆ GetCurrentUser()

static GrimaUser::GetCurrentUser ( )
static

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

References $grima, and FromArray().

3325  {
3326  global $grima;
3327  $grima->session_init();
3328  session_write_close();
3329  if (!isset($_SESSION)) return false;
3330  return GrimaUser::FromArray( $_SESSION );
3331  }
$grima
Definition: grima-lib.php:1166
static FromArray( $data)
Definition: grima-lib.php:3234

◆ LookupUser()

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

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

References GrimaDB\$db, and FromArray().

Referenced by SetCurrentUser().

3242  {
3243  $db = self::getDb();
3244  $query = $db->prepare(
3245  'SELECT * ' .
3246  'FROM institutions NATURAL JOIN users '.
3247  'WHERE institution=:institution '.
3248  'AND username=:username'
3249  );
3250  $success = $query->execute( array(
3251  'institution' => $institution,
3252  'username' => $username,
3253  ) );
3254  if ($success) {
3255  $row = $query->fetch(PDO::FETCH_ASSOC);
3256  if ($row===false) return false;
3257  $user = GrimaUser::FromArray( $row );
3258  if ( ($password !== FALSE) && ($user['password']!='') ) {
3259  if ( !password_verify( $password, $user['password'] ) ) return false;
3260  if ( password_needs_rehash( $user['password'], $user->getPasswordAlgorithm() ) ) {
3261  $user['password'] = $password;
3262  $user->updateDB();
3263  }
3264  }
3265  unset( $user['password'] );
3266  return $user;
3267  } else {
3268  $errorCode = $query->errorCode();
3269  $errorInfo = $query->errorInfo();
3270  throw new Exception(
3271  "Could not select from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3272  );
3273  }
3274  }
static FromArray( $data)
Definition: grima-lib.php:3234
static $db
Definition: grima-lib.php:3170

◆ RenameUser()

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

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

References GrimaDB\$db.

3276  {
3277  $db = self::getDb();
3278  $query = $db->prepare(
3279  'UPDATE users ' .
3280  'SET username=:newusername ' .
3281  'WHERE institution=:institution '.
3282  'AND username=:username'
3283  );
3284  $success = $query->execute( array(
3285  'institution' => $institution,
3286  'username' => $username,
3287  'newusername' => $newusername,
3288  ) );
3289  if ($success) {
3290  return true;
3291  } else {
3292  $errorCode = $query->errorCode();
3293  $errorInfo = $query->errorInfo();
3294  throw new Exception(
3295  "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3296  );
3297  }
3298  }
static $db
Definition: grima-lib.php:3170

◆ ResetPassword()

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

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

References GrimaDB\$db.

3300  {
3301  $db = self::getDb();
3302  $query = $db->prepare(
3303  'UPDATE users ' .
3304  'SET password=:password ' .
3305  'WHERE institution=:institution '.
3306  'AND username=:username'
3307  );
3308  $passwordHash = password_hash( $password, self::getPasswordAlgorithm() );
3309  $success = $query->execute( array(
3310  'institution' => $institution,
3311  'username' => $username,
3312  'password' => $passwordHash,
3313  ) );
3314  if ($success) {
3315  return true;
3316  } else {
3317  $errorCode = $query->errorCode();
3318  $errorInfo = $query->errorInfo();
3319  throw new Exception(
3320  "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3321  );
3322  }
3323  }
static $db
Definition: grima-lib.php:3170

◆ SetCurrentUser()

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

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

References $grima, and LookupUser().

3333  {
3334  global $grima;
3335  $user = GrimaUser::LookupUser( $username, $institution, $password );
3336  if ( $user !== false ) {
3337  if (isset($_SESSION)) {
3338  session_write_close();
3339  session_start();
3340  }
3341  $grima->session_save($user);
3342  } else {
3343  return false;
3344  }
3345  }
$grima
Definition: grima-lib.php:1166
static LookupUser($username, $institution='', $password=FALSE)
Definition: grima-lib.php:3242

◆ updateDB()

GrimaUser::updateDB ( )

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

References GrimaDB\$db, GrimaDB\getDb(), and GrimaDB\getPasswordAlgorithm().

3372  {
3373  $db = $this->getDb();
3374  $query = $db->prepare( 'UPDATE users SET isAdmin=:isAdmin, password=:password WHERE username=:username AND institution=:institution' );
3375  if (!$query) {
3376  $errorCode = $db->errorCode();
3377  $errorInfo = $db->errorInfo();
3378  throw new Exception(
3379  "Could not even prepare to update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3380  );
3381  }
3382  $success = $query->execute( array(
3383  'username' => $this['username'],
3384  'password' => password_hash( $this['password'], $this->getPasswordAlgorithm() ),
3385  'institution' => $this['institution'],
3386  'isAdmin' => $this['isAdmin'],
3387  ) );
3388  if (!$success) {
3389  $errorCode = $query->errorCode();
3390  $errorInfo = $query->errorInfo();
3391  throw new Exception(
3392  "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
3393  );
3394  }
3395  }
getPasswordAlgorithm()
Definition: grima-lib.php:3166
static getDb()
Definition: grima-lib.php:3158
static $db
Definition: grima-lib.php:3170

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