Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Comments should be used sparingly.
* Empty lines should be used sparingly.
* No code should call `die()` or `exit()`, instead `UnitySite::die()`. This will avoid the premature death of our automated testing processes.
* Instead of `assert`, use `\ensure`. This will enforce conditions even in production.

This repository will automatically check PRs for linting compliance.

Expand Down
1 change: 1 addition & 0 deletions resources/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require_once __DIR__ . "/lib/exceptions/NoDieException.php";
require_once __DIR__ . "/lib/exceptions/SSOException.php";
require_once __DIR__ . "/lib/exceptions/ArrayKeyException.php";
require_once __DIR__ . "/lib/exceptions/EnsureException.php";

require_once __DIR__ . "/config.php";
require __DIR__ . "/init.php";
4 changes: 2 additions & 2 deletions resources/lib/UnityGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
// $users = $this->getGroupMembers();

// // now we delete the ldap entry
// assert($this->entry->exists());
// \ensure($this->entry->exists());
// $this->entry->delete();
// $this->REDIS->removeCacheArray("sorted_groups", "", $this->gid);
// foreach ($users as $user) {
Expand Down Expand Up @@ -463,7 +463,7 @@ public function requestExists($user)
private function init()
{
$owner = $this->getOwner();
assert(!$this->entry->exists());
\ensure(!$this->entry->exists());
$nextGID = $this->LDAP->getNextPIGIDNumber();
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$this->entry->setAttribute("gidnumber", strval($nextGID));
Expand Down
2 changes: 1 addition & 1 deletion resources/lib/UnityOrg.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)

public function init()
{
assert(!$this->entry->exists());
\ensure(!$this->entry->exists());
$nextGID = $this->LDAP->getNextOrgGIDNumber($this->SQL);
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$this->entry->setAttribute("gidnumber", strval($nextGID));
Expand Down
26 changes: 13 additions & 13 deletions resources/lib/UnityUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public function init($firstname, $lastname, $email, $org, $send_mail = true)
{
$ldapGroupEntry = $this->getGroupEntry();
$id = $this->LDAP->getNextUIDGIDNumber($this->uid);
assert(!$ldapGroupEntry->exists());
\ensure(!$ldapGroupEntry->exists());
$ldapGroupEntry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$ldapGroupEntry->setAttribute("gidnumber", strval($id));
$ldapGroupEntry->write();

assert(!$this->entry->exists());
\ensure(!$this->entry->exists());
$this->entry->setAttribute("objectclass", UnityLDAP::POSIX_ACCOUNT_CLASS);
$this->entry->setAttribute("uid", $this->uid);
$this->entry->setAttribute("givenname", $firstname);
Expand Down Expand Up @@ -145,7 +145,7 @@ public function setOrg($org)

public function getOrg($ignorecache = false)
{
assert($this->exists());
\ensure($this->exists());
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->uid, "org");
if (!is_null($cached_val)) {
Expand Down Expand Up @@ -194,7 +194,7 @@ public function setFirstname($firstname, $operator = null)
*/
public function getFirstname($ignorecache = false)
{
assert($this->exists());
\ensure($this->exists());
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->uid, "firstname");
if (!is_null($cached_val)) {
Expand Down Expand Up @@ -243,7 +243,7 @@ public function setLastname($lastname, $operator = null)
*/
public function getLastname($ignorecache = false)
{
assert($this->exists());
\ensure($this->exists());
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->uid, "lastname");
if (!is_null($cached_val)) {
Expand All @@ -266,7 +266,7 @@ public function getLastname($ignorecache = false)

public function getFullname()
{
assert($this->exists());
\ensure($this->exists());
return $this->getFirstname() . " " . $this->getLastname();
}

Expand Down Expand Up @@ -298,7 +298,7 @@ public function setMail($email, $operator = null)
*/
public function getMail($ignorecache = false)
{
assert($this->exists());
\ensure($this->exists());
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->uid, "mail");
if (!is_null($cached_val)) {
Expand Down Expand Up @@ -328,7 +328,7 @@ public function setSSHKeys($keys, $operator = null, $send_mail = true)
{
$operator = is_null($operator) ? $this->uid : $operator->uid;
$keys_filt = array_values(array_unique($keys));
assert($this->entry->exists());
\ensure($this->entry->exists());
$this->entry->setAttribute("sshpublickey", $keys_filt);
$this->entry->write();

Expand Down Expand Up @@ -357,7 +357,7 @@ public function setSSHKeys($keys, $operator = null, $send_mail = true)
*/
public function getSSHKeys($ignorecache = false)
{
assert($this->exists());
\ensure($this->exists());
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->uid, "sshkeys");
if (!is_null($cached_val)) {
Expand Down Expand Up @@ -400,7 +400,7 @@ public function setLoginShell($shell, $operator = null, $send_mail = true)
if (empty($shell)) {
throw new Exception("login shell must not be empty!");
}
assert($this->entry->exists());
\ensure($this->entry->exists());
$this->entry->setAttribute("loginshell", $shell);
$this->entry->write();

Expand Down Expand Up @@ -431,7 +431,7 @@ public function setLoginShell($shell, $operator = null, $send_mail = true)
*/
public function getLoginShell($ignorecache = false)
{
assert($this->exists());
\ensure($this->exists());
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->uid, "loginshell");
if (!is_null($cached_val)) {
Expand All @@ -454,7 +454,7 @@ public function getLoginShell($ignorecache = false)

public function setHomeDir($home, $operator = null)
{
assert($this->entry->exists());
\ensure($this->entry->exists());
$this->entry->setAttribute("homedirectory", $home);
$this->entry->write();
$operator = is_null($operator) ? $this->uid : $operator->uid;
Expand All @@ -476,7 +476,7 @@ public function setHomeDir($home, $operator = null)
*/
public function getHomeDir($ignorecache = false)
{
assert($this->exists());
\ensure($this->exists());
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->uid, "homedir");
if (!is_null($cached_val)) {
Expand Down
7 changes: 7 additions & 0 deletions resources/lib/exceptions/EnsureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace UnityWebPortal\lib\exceptions;

class EnsureException extends \Exception
{
}
9 changes: 9 additions & 0 deletions resources/lib/utils.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use UnityWebPortal\lib\exceptions\ArrayKeyException;
use UnityWebPortal\lib\exceptions\EnsureException;

function arrayGet($array, ...$keys)
{
Expand All @@ -19,3 +20,11 @@ function arrayGet($array, ...$keys)
}
return $cursor;
}

// like assert() but not subject to zend.assertions config
function ensure(bool $condition, ?string $message = null)
{
if (!$condition) {
throw new EnsureException($message ?? "ensure condition is false");
}
}
1 change: 1 addition & 0 deletions test/phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require_once __DIR__ . "/../resources/lib/exceptions/NoDieException.php";
require_once __DIR__ . "/../resources/lib/exceptions/SSOException.php";
require_once __DIR__ . "/../resources/lib/exceptions/ArrayKeyException.php";
require_once __DIR__ . "/../resources/lib/exceptions/EnsureException.php";

$_SERVER["HTTP_HOST"] = "phpunit"; // used for config override
require_once __DIR__ . "/../resources/config.php";
Expand Down