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

Shared access to the database for GrimaUser and GrimaInstitution. More...

Inheritance diagram for GrimaDB:
GrimaInstitution GrimaUser

Public Member Functions

  offsetExists ($offset)
 
  offsetGet ($offset)
 
  offsetSet ($offset, $value)
 
  offsetUnset ($offset)
 
  getIterator ()
 

Static Public Member Functions

static  init ()
 
static  isEmpty ()
 
static  isStateless ()
 
static  getInstitutions ()
 

Protected Member Functions

  getPasswordAlgorithm ()
 

Static Protected Member Functions

static  getDb ()
 

Private Attributes

  $info
 

Static Private Attributes

static  $db = false
 

Detailed Description

Shared access to the database for GrimaUser and GrimaInstitution.

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

Member Function Documentation

◆ getDb()

static GrimaDB::getDb ( )
staticprotected

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

4696 {
4697 if (!self::$db) {
4698 try {
4699 $db_url = getenvWithFileFallbackAndDefault('DATABASE_URL', '');
4700 $db_user = getenvWithFileFallbackAndDefault('DATABASE_USER', '');
4701 $db_pass = getenvWithFileFallbackAndDefault('DATABASE_PASS', '');
4702 if ($db_user && $db_pass) {
4703 self::$db = new PDO($db_url, $db_user, $db_pass);
4704 } else {
4705 self::$db = new PDO($db_url);
4706 }
4707 if (!tableExists(self::$db, "institutions")) {
4708 self::$db->exec("CREATE TABLE institutions ( institution VARCHAR(100) PRIMARY KEY, apikey VARCHAR(100), server VARCHAR(100) )");
4709 }
4710 if (!tableExists(self::$db, "users")) {
4711 self::$db->exec("CREATE TABLE users ( institution VARCHAR(100), username VARCHAR(100), password VARCHAR(255), isAdmin INTEGER )");
4712 }
4713 } catch (Exception $e) {
4714 self::$db = 'stateless';
4715 }
4716 }
4717 return self::$db;
4718 }
static $db
Definition: grima-lib.php:4665
tableExists($pdo, $table)
Definition: grima-lib.php:4642
getenvWithFileFallbackAndDefault( $env_name, $default=false)
Definition: grima-util.php:52

References $db, getenvWithFileFallbackAndDefault(), and tableExists().

Referenced by GrimaInstitution\addToDB(), GrimaUser\addToDB(), GrimaUser\deleteFromDB(), getInstitutions(), init(), isEmpty(), isStateless(), GrimaUser\LookupUser(), GrimaUser\RenameUser(), GrimaUser\ResetPassword(), and GrimaUser\updateDB().

◆ getInstitutions()

static GrimaDB::getInstitutions ( )
static

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

4726 {
4727 $ret = array();
4728 try {
4729 $db = self::getDb();
4730 if ($db === 'stateless') {
4731 return $ret;
4732 }
4733 $result = $db->query('SELECT institution FROM institutions');
4734 foreach ($result as $row) {
4735 $ret[] = $row['institution'];
4736 }
4737 } catch (Exception $e) {
4738 }
4739 return $ret;
4740 }
static getDb()
Definition: grima-lib.php:4695

References $db, and getDb().

Referenced by GrimaFormField\__construct().

◆ getIterator()

GrimaDB::getIterator ( )

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

4763 {
4764 return new ArrayIterator($this->info);
4765 }

◆ getPasswordAlgorithm()

GrimaDB::getPasswordAlgorithm ( )
protected

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

4721 {
4722 return defined("PASSWORD_ARGON2I") ? constant("PASSWORD_ARGON2I") : PASSWORD_DEFAULT;
4723 }

Referenced by GrimaUser\addToDB(), and GrimaUser\updateDB().

◆ init()

static GrimaDB::init ( )
static

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

4669 {
4670 return self::getDb();
4671 }

References getDb().

◆ isEmpty()

static GrimaDB::isEmpty ( )
static

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

4674 {
4675 $db = self::getDb();
4676 if ($db === 'stateless') {
4677 return true;
4678 }
4679 $result = $db->query('SELECT COUNT(*) as c FROM institutions');
4680 if ($result) {
4681 foreach ($result as $row) {
4682 if ($row['c']>0) { return false;
4683 }
4684 }
4685 }
4686 return true;
4687 }

References $db, and getDb().

◆ isStateless()

static GrimaDB::isStateless ( )
static

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

4690 {
4691 $db = self::getDb();
4692 return $db === 'stateless';
4693 }

References $db, and getDb().

◆ offsetExists()

GrimaDB::offsetExists (   $offset )

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

4743 {
4744 return isset($this->info[$offset]);
4745 }

◆ offsetGet()

GrimaDB::offsetGet (   $offset )

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

4748 {
4749 return isset($this->info[$offset]) ? $this->info[$offset] : '';
4750 }

◆ offsetSet()

GrimaDB::offsetSet (   $offset,
  $value 
)

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

4753 {
4754 $this->info[$offset] = $value;
4755 }

◆ offsetUnset()

GrimaDB::offsetUnset (   $offset )

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

4758 {
4759 unset($this->info[$offset]);
4760 }

Member Data Documentation

◆ $db

GrimaDB::$db = false
staticprivate

◆ $info

GrimaDB::$info
private

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


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