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

Member Function Documentation

◆ addToDB()

GrimaUser::addToDB ( )

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

4955 {
4956 $db = $this->getDb();
4957 if ($db === 'stateless') {
4958 throw new Exception("No database specified. Grima cannot add a user to the database, if it doesn't where to add it.");
4959 }
4960 $query = $db->prepare('INSERT INTO users( username, password, institution, isAdmin ) VALUES (:username, :password, :institution, :isAdmin)');
4961 if (!$query) {
4962 $errorCode = $db->errorCode();
4963 $errorInfo = $db->errorInfo();
4964 throw new Exception(
4965 "Could not even prepare to insert into user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4966 );
4967 }
4968 $success = $query->execute(
4969 array(
4970 'username' => $this['username'],
4971 'password' => password_hash($this['password'], $this->getPasswordAlgorithm()),
4972 'institution' => $this['institution'],
4973 'isAdmin' => $this['isAdmin'],
4974 )
4975 );
4976 if (!$success) {
4977 $errorCode = $query->errorCode();
4978 $errorInfo = $query->errorInfo();
4979 throw new Exception(
4980 "Could not insert into user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4981 );
4982 }
4983 }
getPasswordAlgorithm()
Definition: grima-lib.php:4720
static getDb()
Definition: grima-lib.php:4695
static $db
Definition: grima-lib.php:4665

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

◆ deleteFromDB()

GrimaUser::deleteFromDB ( )

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

5017 {
5018 $db = $this->getDb();
5019 if ($db === 'stateless') {
5020 throw new Exception("No database specified. Grima cannot save changes to users if it doesn't know where to save them.");
5021 }
5022 $query = $db->prepare('DELETE FROM users WHERE username=:username AND institution=:institution');
5023 if (!$query) {
5024 $errorCode = $db->errorCode();
5025 $errorInfo = $db->errorInfo();
5026 throw new Exception(
5027 "Could not even prepare to delete from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
5028 );
5029 }
5030 $success = $query->execute(
5031 array(
5032 'username' => $this['username'],
5033 'institution' => $this['institution'],
5034 )
5035 );
5036 if (!$success) {
5037 $errorCode = $query->errorCode();
5038 $errorInfo = $query->errorInfo();
5039 throw new Exception(
5040 "Could not delete from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
5041 );
5042 }
5043 }

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

◆ FromArray()

static GrimaUser::FromArray (   $data )
static

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

4819 {
4820 $user = new GrimaUser();
4821 foreach ($data as $key => $val) {
4822 $user[$key] = $val;
4823 }
4824 return $user;
4825 }
Interface to the GrimaDB database to check password and get institution's apikey.
Definition: grima-lib.php:4817

Referenced by GetCurrentUser(), and LookupUser().

◆ GetCurrentUser()

static GrimaUser::GetCurrentUser ( )
static

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

4931 {
4932 if (!isset($_SESSION)) { return false;
4933 }
4934 return GrimaUser::FromArray($_SESSION);
4935 }
static FromArray($data)
Definition: grima-lib.php:4818

References FromArray().

Referenced by GrimaFormField\__construct().

◆ LogoutCurrentUser()

static GrimaUser::LogoutCurrentUser ( )
static

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

4949 {
4950 global $grima;
4951 $grima->sessionDestroy();
4952 }
$grima
Definition: grima-lib.php:5143

References $grima.

◆ LookupUser()

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

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

4828 {
4829 $db = self::getDb();
4830 if ($db === 'stateless') {
4831 return false;
4832 }
4833 $query = $db->prepare(
4834 'SELECT * ' .
4835 'FROM institutions NATURAL JOIN users '.
4836 'WHERE institution=:institution '.
4837 'AND username=:username'
4838 );
4839 $success = $query->execute(
4840 array(
4841 'institution' => $institution,
4842 'username' => $username,
4843 )
4844 );
4845 if ($success) {
4846 $row = $query->fetch(PDO::FETCH_ASSOC);
4847 if ($row===false) { return false;
4848 }
4849 $user = GrimaUser::FromArray($row);
4850 if (($password !== false) && ($user['password']!='')) {
4851 if (!password_verify($password, $user['password'])) { return false;
4852 }
4853 if (password_needs_rehash($user['password'], $user->getPasswordAlgorithm())) {
4854 $user['password'] = $password;
4855 $user->updateDB();
4856 }
4857 }
4858 unset($user['password']);
4859 return $user;
4860 } else {
4861 $errorCode = $query->errorCode();
4862 $errorInfo = $query->errorInfo();
4863 throw new Exception(
4864 "Could not select from user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4865 );
4866 }
4867 }

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

Referenced by SetCurrentUser().

◆ RenameUser()

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

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

4870 {
4871 $db = self::getDb();
4872 if ($db === 'stateless') {
4873 throw new Exception("No database has been specified. Grima cannot save changes to users if it does not where to save them.");
4874 }
4875 $query = $db->prepare(
4876 'UPDATE users ' .
4877 'SET username=:newusername ' .
4878 'WHERE institution=:institution '.
4879 'AND username=:username'
4880 );
4881 $success = $query->execute(
4882 array(
4883 'institution' => $institution,
4884 'username' => $username,
4885 'newusername' => $newusername,
4886 )
4887 );
4888 if ($success) {
4889 return true;
4890 } else {
4891 $errorCode = $query->errorCode();
4892 $errorInfo = $query->errorInfo();
4893 throw new Exception(
4894 "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4895 );
4896 }
4897 }

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

◆ ResetPassword()

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

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

4900 {
4901 $db = self::getDb();
4902 if ($db === 'stateless') {
4903 throw new Exception("No database specified. Grima cannot save changes to users if it doesn't know where to save them.");
4904 }
4905 $query = $db->prepare(
4906 'UPDATE users ' .
4907 'SET password=:password ' .
4908 'WHERE institution=:institution '.
4909 'AND username=:username'
4910 );
4911 $passwordHash = password_hash($password, self::getPasswordAlgorithm());
4912 $success = $query->execute(
4913 array(
4914 'institution' => $institution,
4915 'username' => $username,
4916 'password' => $passwordHash,
4917 )
4918 );
4919 if ($success) {
4920 return true;
4921 } else {
4922 $errorCode = $query->errorCode();
4923 $errorInfo = $query->errorInfo();
4924 throw new Exception(
4925 "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4926 );
4927 }
4928 }

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

◆ SetCurrentUser()

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

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

4938 {
4939 global $grima;
4940 $user = GrimaUser::LookupUser($username, $institution, $password);
4941 if ($user !== false) {
4942 $grima->sessionSave($user);
4943 } else {
4944 return false;
4945 }
4946 }
static LookupUser($username, $institution='', $password=false)
Definition: grima-lib.php:4827

References $grima, and LookupUser().

◆ updateDB()

GrimaUser::updateDB ( )

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

4986 {
4987 $db = $this->getDb();
4988 if ($db === 'stateless') {
4989 throw new Exception("No database specified. Grima cannot save changes to users if it doesn't know where to save them.");
4990 }
4991 $query = $db->prepare('UPDATE users SET isAdmin=:isAdmin, password=:password WHERE username=:username AND institution=:institution');
4992 if (!$query) {
4993 $errorCode = $db->errorCode();
4994 $errorInfo = $db->errorInfo();
4995 throw new Exception(
4996 "Could not even prepare to update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
4997 );
4998 }
4999 $success = $query->execute(
5000 array(
5001 'username' => $this['username'],
5002 'password' => password_hash($this['password'], $this->getPasswordAlgorithm()),
5003 'institution' => $this['institution'],
5004 'isAdmin' => $this['isAdmin'],
5005 )
5006 );
5007 if (!$success) {
5008 $errorCode = $query->errorCode();
5009 $errorInfo = $query->errorInfo();
5010 throw new Exception(
5011 "Could not update user database: [$errorCode] {$errorInfo[0]} {$errorInfo[2]}"
5012 );
5013 }
5014 }

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


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