Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.12.0 -- Add Group Aliases #116

Merged
merged 12 commits into from
Nov 14, 2024
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ RUN apt-get update -y \
&& echo "America/New_York" > /etc/timezone \
&& apt-get install -y tzdata \
&& apt-get upgrade -y \
# Install some basics
&& apt-get install -y zip unzip wget \
# Needed for GoogleMock objects
# Install
&& apt-get install -y \
# things needed for GoogleMock objects
sqlite3 \
# some basics
unzip wget zip \
# Clean up to reduce docker image size
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
Expand Down
47 changes: 23 additions & 24 deletions SilMock/DataStore/Sqlite/SqliteUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@

class SqliteUtils
{

/**
* The PDO connection to the database (or null if unititialized).
* @var PDO|null
*/
private $_db = null;
private $db = null;

/**
* The SQLite database file (path with file name).
* @var string
*/
private $_dbFile;
private $dbFile;

private $_dbTable = 'google_service';
private $dbTable = 'google_service';

/**
*
Expand All @@ -29,11 +28,11 @@ class SqliteUtils
public function __construct($dbFile = null)
{
// default database path
$this->_dbFile = __DIR__ . '/Google_Service_Data.db';
$this->dbFile = __DIR__ . '/Google_Service_Data.db';

// if database path given, use it instead
if ($dbFile) {
$this->_dbFile = $dbFile;
$this->dbFile = $dbFile;
}

$this->createDbStructureAsNecessary();
Expand All @@ -52,7 +51,7 @@ public function __construct($dbFile = null)
*/
public function getData($dataType, $dataClass)
{
if (! file_exists($this->_dbFile)) {
if (! file_exists($this->dbFile)) {
return null;
}

Expand All @@ -71,15 +70,15 @@ public function getData($dataType, $dataClass)

if (! $whereClause) {
return $this->runSql(
"SELECT * FROM " . $this->_dbTable,
"SELECT * FROM " . $this->dbTable,
array(),
false,
true
);
}

return $this->runSql(
"SELECT * FROM " . $this->_dbTable . $whereClause,
"SELECT * FROM " . $this->dbTable . $whereClause,
$whereArray,
false,
true
Expand Down Expand Up @@ -144,7 +143,7 @@ public function getAllRecordsByDataKey($dataType, $dataClass, $dataKey, $dataVal
public function deleteRecordById($recordId)
{
$this->runSql(
"DELETE FROM " . $this->_dbTable . " WHERE id = :id",
"DELETE FROM " . $this->dbTable . " WHERE id = :id",
[':id' => $recordId],
true
);
Expand All @@ -164,7 +163,7 @@ public function deleteRecordById($recordId)
*/
public function deleteDataByEmail(string $dataType, string $dataClass, string $emailAddress)
{
if (! file_exists($this->_dbFile)) {
if (! file_exists($this->dbFile)) {
return null;
}
if (empty($dataType)) {
Expand All @@ -187,12 +186,12 @@ public function deleteDataByEmail(string $dataType, string $dataClass, string $e
* the input value
* @param $recordId int
* @param $newData string
* @return null
* @return void
*/
public function updateRecordById($recordId, $newData)
public function updateRecordById($recordId, $newData): void
{
$this->runSql(
"UPDATE " . $this->_dbTable . " SET data = :data WHERE id = :id",
"UPDATE " . $this->dbTable . " SET data = :data WHERE id = :id",
[':id' => $recordId, ':data' => $newData],
true
);
Expand All @@ -205,7 +204,7 @@ public function updateRecordById($recordId, $newData)
public function deleteAllData()
{
return $this->runSql(
"DELETE FROM " . $this->_dbTable . " WHERE id > -1"
"DELETE FROM " . $this->dbTable . " WHERE id > -1"
);
}

Expand All @@ -231,7 +230,7 @@ public function recordData($dataType, $dataClass, $data)

// Add the record.
$this->runSql(
'INSERT INTO ' . $this->_dbTable . ' (type, class, data)' .
'INSERT INTO ' . $this->dbTable . ' (type, class, data)' .
' VALUES (:type, :class, :data)',
[':type' => $dataType, ':class' => $dataClass, ':data' => $data],
true
Expand All @@ -247,9 +246,9 @@ public function recordData($dataType, $dataClass, $data)
*/
public function createDbIfNotExists()
{
if (! file_exists($this->_dbFile)) {
file_put_contents($this->_dbFile, '');
chmod($this->_dbFile, 0644);
if (! file_exists($this->dbFile)) {
file_put_contents($this->dbFile, '');
chmod($this->dbFile, 0644);
}
}

Expand All @@ -266,7 +265,7 @@ public function createDbStructureAsNecessary()
$this->createDbIfNotExists();

$this->runSql(
"CREATE TABLE IF NOT EXISTS " . $this->_dbTable . " (" .
"CREATE TABLE IF NOT EXISTS " . $this->dbTable . " (" .
"id INTEGER PRIMARY KEY, " .
"type TEXT, " . // e.g. "directory"
"class TEXT, " . // e.g. "user"
Expand Down Expand Up @@ -310,7 +309,7 @@ protected function runSql(
$this->setupDbConnIfNeeded();

// Update the record in the database.
$stmt = $this->_db->prepare($sql);
$stmt = $this->db->prepare($sql);

// Execute the prepared update statement with the desired data.
$stmtSuccess = $stmt->execute($data);
Expand Down Expand Up @@ -340,15 +339,15 @@ protected function runSql(
protected function setupDbConnIfNeeded()
{
// If we have not yet setup the database connection...
if (is_null($this->_db)) {
if (is_null($this->db)) {
// Make sure the database itself exists.
$this->createDbIfNotExists();

// Connect to the SQLite database file.
$this->_db = new PDO('sqlite:' . $this->_dbFile);
$this->db = new PDO('sqlite:' . $this->dbFile);

// Set errormode to exceptions.
$this->_db->setAttribute(
$this->db->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
);
Expand Down
3 changes: 3 additions & 0 deletions SilMock/Google/Service/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilMock\Google\Http\Batch;
use SilMock\Google\Service\Directory\Asps;
use SilMock\Google\Service\Directory\Resource\Groups;
use SilMock\Google\Service\Directory\Resource\GroupsAliases;
use SilMock\Google\Service\Directory\Resource\Members;
use SilMock\Google\Service\Directory\Resource\TwoStepVerification;
use SilMock\Google\Service\Directory\Tokens;
Expand All @@ -19,6 +20,7 @@ class Directory
public $asps;
public Members $members;
public Groups $groups;
public GroupsAliases $groups_aliases;
public $tokens;
public $users;
public $users_aliases;
Expand All @@ -38,6 +40,7 @@ public function __construct($client, ?string $dbFile = null)
$this->asps = new Asps($dbFile);
$this->members = new Members($dbFile);
$this->groups = new Groups($dbFile);
$this->groups_aliases = new GroupsAliases($dbFile);
$this->tokens = new Tokens($dbFile);
$this->users = new UsersResource($dbFile);
$this->users_aliases = new UsersAliasesResource($dbFile);
Expand Down
6 changes: 2 additions & 4 deletions SilMock/Google/Service/Directory/Asps.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ public function __construct(?string $dbFile = null)
{
parent::__construct($dbFile, 'directory', 'asps');
}

public function listAsps($userKey, $optParams = array())
{
return new Google_Service_Directory_Asps(array(
'items' => array(),
));
return new Google_Service_Directory_Asps(['items' => []]);
}
}
3 changes: 2 additions & 1 deletion SilMock/Google/Service/Directory/Resource/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function get(string $groupKey): ?GoogleDirectory_Group
public function insert(GoogleDirectory_Group $postBody, $optParams = [])
{
if ($this->isNewGroup($postBody->getEmail())) {
$postBody['id'] = $postBody['id'] ?? microtime();
$id = str_replace(array(' ', '.'), '', microtime());
$postBody['id'] = $postBody['id'] ?? $id;
$dataAsJson = json_encode(get_object_vars($postBody));
$this->addRecord($dataAsJson);
}
Expand Down
92 changes: 92 additions & 0 deletions SilMock/Google/Service/Directory/Resource/GroupsAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace SilMock\Google\Service\Directory\Resource;

use Exception;
use Google\Service\Directory\Alias as GoogleDirectory_GroupsAlias;
use Google\Service\Directory\Aliases as GoogleDirectory_GroupsAliases;
use SilMock\Google\Service\DbClass;
use SilMock\Google\Service\Directory\ObjectUtils;

class GroupsAliases extends DbClass
{
public function __construct(?string $dbFile = null)
{
parent::__construct($dbFile, 'directory', 'groupsaliases');
}

public function delete(string $groupKey, string $alias, array $optParams = [])
{
$groupAliasRecords = $this->getRecords();
foreach ($groupAliasRecords as $groupAliasRecord) {
$groupAliasRecordData = json_decode($groupAliasRecord['data'], true);
if (
$groupAliasRecordData['primaryEmail'] === $groupKey
&& $groupAliasRecordData['alias'] === $alias
) {
$this->deleteRecordById($groupAliasRecordData['id']);
}
}
}

/**
* @throws Exception
*/
public function insert(
string $groupKey,
GoogleDirectory_GroupsAlias $postBody,
array $optParams = []
): GoogleDirectory_GroupsAlias {
if ($this->isNewGroupAlias($groupKey, $postBody->getAlias())) {
$id = str_replace(array(' ', '.'), '', microtime());
$postBody['id'] = $postBody['id'] ?? $id;
$dataAsJson = json_encode(get_object_vars($postBody));
$this->addRecord($dataAsJson);
}

$newGroupAlias = new GoogleDirectory_GroupsAlias();
ObjectUtils::initialize($newGroupAlias, $postBody);

return $newGroupAlias;
}

/**
* @param $optParams -- Initial implementation ignores this.
* @return GoogleDirectory_GroupsAliases
*/
public function listGroupsAliases(string $groupKey, array $optParams = []): GoogleDirectory_GroupsAliases
{
$groupAliases = [];
$groupAliasRecords = $this->getRecords();
$groupAliasCounter = 0;
foreach ($groupAliasRecords as $groupAliasRecord) {
$groupRecordData = json_decode($groupAliasRecord['data'], true);
if ($groupRecordData['primaryEmail'] === $groupKey) {
$currentGroup = new GoogleDirectory_GroupsAlias();
ObjectUtils::initialize($currentGroup, $groupRecordData);
$groupAliases[] = $currentGroup;
$groupAliasCounter = $groupAliasCounter + 1;
}
}
$groupsAliasesObject = new GoogleDirectory_GroupsAliases();
$groupsAliasesObject->setEtag('');
$groupsAliasesObject->setKind('groupAliases');
$groupsAliasesObject->setAliases($groupAliases);
return $groupsAliasesObject;
}

protected function isNewGroupAlias(string $groupKey, string $alias): bool
{
$mockGroupsObject = new GroupsAliases($this->dbFile);
$groupsAliasesObject = $mockGroupsObject->listGroupsAliases($groupKey);
/** @var GoogleDirectory_GroupsAlias[] $aliasObjects */
$aliasObjects = $groupsAliasesObject->getAliases();
$lowercaseAliasEmailAddress = mb_strtolower($alias);
foreach ($aliasObjects as $aliasObject) {
if (mb_strtolower($aliasObject->getAlias()) === $lowercaseAliasEmailAddress) {
return false;
}
}
return true;
}
}
7 changes: 6 additions & 1 deletion SilMock/Google/Service/Directory/Resource/Members.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ protected function extractRoles(?string $roles): array
{
if (! empty($roles)) {
$allExpectedRoles = explode(',', $roles);
$expectedRoles = array_map(function ($role) { return mb_strtoupper(trim($role)); }, $allExpectedRoles);
$expectedRoles = array_map(
function ($role) {
return mb_strtoupper(trim($role));
},
$allExpectedRoles
);
} else {
$expectedRoles = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ public function turnOff($userKey, $optParams = [])
$sqliteUtils->updateRecordById($recordId, json_encode($twoStepVerificationRecord));
}
}
}
}
2 changes: 1 addition & 1 deletion SilMock/Google/Service/Directory/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(?string $dbFile = null)
{
parent::__construct($dbFile, 'directory', 'tokens');
}

public function listTokens($userKey, $optParams = []): Google_Service_Directory_Tokens
{
return new Google_Service_Directory_Tokens([ 'items' => [] ]);
Expand Down
Loading