Skip to content

Remove getters #268

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions resources/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
require_once __DIR__ . "/lib/phpopenldaper/src/PHPOpenLDAPer/LDAPConn.php";

// load libs
require_once __DIR__ . "/lib/ObjectClass.php";
require_once __DIR__ . "/lib/ObjectClassUser.php";
require_once __DIR__ . "/lib/ObjectClassGroup.php";
require_once __DIR__ . "/lib/UnityLDAP.php";
require_once __DIR__ . "/lib/UnityUser.php";
require_once __DIR__ . "/lib/UnityGroup.php";
Expand Down
4 changes: 2 additions & 2 deletions resources/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
$SEND_PIMESG_TO_ADMINS = $CONFIG["mail"]["send_pimesg_to_admins"];

$SQL->addLog(
$OPERATOR->getUID(),
$OPERATOR->uid,
$_SERVER['REMOTE_ADDR'],
"user_login",
$OPERATOR->getUID()
$OPERATOR->uid
);

if (!$_SESSION["user_exists"]) {
Expand Down
69 changes: 69 additions & 0 deletions resources/lib/ObjectClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace UnityWebPortal\lib;

use Exception;

/**
* Uses magic __get to return $this->entry->getAttribute($x) or $this->entry->getAttribute($x)[0]
* $attributes_array is a list of LDAP attribute names (lowercase only!) that should be an array
* $attributes_non_array is a list of LDAP attribute names (lowercase only!) that should be a single
* value instead of an array
* $entry is a PHPOpenLDAPer\LDAPEntry
* @since 8.3.0
*/
class ObjectClass
{
private ?LDAPEntry $entry = null; // define in constructor of child class
protected static array $attributes_array = [];
protected static array $attributes_non_array = [];
private $validated = false;

private function ensureAttributeListsValidated()
{
if ($this->validated) {
return;
}
assert(
array_reduce(
array_merge(static::$attributes_array, static::$attributes_non_array),
fn($carry, $x) => $carry && is_string($x) && $x === strtolower($x),
true
),
"attributes_array and attributes_non_array must be only lowercase strings"
);
$this->validated = true;
}

public function __get(string $property): mixed
{
assert(!is_null($this->entry));
$this->ensureAttributeListsValidated();
$property = strtolower($property);
if (in_array($property, static::$attributes_array, true)) {
return $this->entry->getAttribute($property);
}
if (in_array($property, static::$attributes_non_array, true)) {
$attribute = $this->entry->getAttribute($property);
if (empty($attribute)) {
throw new AttributeNotFound($property);
}
return $attribute[0];
}
throw new Exception("Unknown property '$property'");
}

public function __isset(string $property): bool
{
$this->ensureAttributeListsValidated();
$property = strtolower($property);
assert(!is_null($this->entry));
$this->assert_attribute_lists_are_lowercase();
if (in_array($property, static::$attributes_array, true)
|| in_array($property, static::$attributes_non_array, true)
) {
return (!empty($this->getAttribute($property)));
}
return false;
}
}
28 changes: 28 additions & 0 deletions resources/lib/ObjectClassGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace UnityWebPortal\lib;

class ObjectClassGroup extends ObjectClass
{
protected static array $attributes_array = ["objectClass", "memberUid"];
protected static array $attributes_non_array = ["cn", "dn", "gidNumber"];
}

// the following will be possible after an upgrade to php 8.4
// class PosixGroup extends \PHPOpenLDAPer\LDAPEntry
// {
// public string $cn {
// get => $this->getAttribute("cn")[0]
// }
// public int $gidNumber {
// get => $this->getAttribute("gidNumber")[0]
// }
// public array $memberUid {
// get => $this->getAttribute("memberUid")
// }
// public array $objectClass {
// get => $this->getAttribute("objectClass")
// }
// }
// $LDAP->getUserGroupEntry,getOrgGroupEntry,getPIGroupEntry will also have to be
// updated to use LDAPConn::getEntryOfObjectClass
67 changes: 67 additions & 0 deletions resources/lib/ObjectClassUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace UnityWebPortal\lib;

class ObjectClassUser extends ObjectClass
{
protected static array $attributes_array = ["objectClass", "sshPublicKey"];
protected static array $attributes_non_array = [
"cn",
"dn",
"gecos",
"gidNumber",
"givenName",
"homeDirectory",
"loginShell",
"mail",
"o",
"sn",
"uid",
"uidNumber"
];
}

// the following will be possible after an upgrade to php 8.4
// class PosixGroup extends \PHPOpenLDAPer\LDAPEntry
// {
// public string $cn {
// get => $this->getAttribute("cn")[0]
// }
// public string $gecos {
// get => $this->getAttribute("gecos")[0]
// }
// public int $gidNumber {
// get => $this->getAttribute("gidNumber")[0]
// }
// public string $givenName {
// get => $this->getAttribute("givenName")[0]
// }
// public string $homeDirectory {
// get => $this->getAttribute("homeDirectory")[0]
// }
// public string $loginShell {
// get => $this->getAttribute("loginShell")[0]
// }
// public string $mail {
// get => $this->getAttribute("mail")[0]
// }
// public string $o {
// get => $this->getAttribute("o")[0]
// }
// public array $objectClass {
// get => $this->getAttribute("objectClass")
// }
// public string $sn {
// get => $this->getAttribute("sn")[0]
// }
// public array $sshPublicKey {
// get => $this->getAttribute("sshPublicKey")
// }
// public string $uid {
// get => $this->getAttribute("uid")[0]
// }
// public int $uidNumber {
// get => $this->getAttribute("uidNumber")[0]
// }
// }
// $LDAP->getUserEntry will also have to be updated to use LDAPConn::getEntryOfObjectClass
Loading