Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberroland committed Nov 4, 2023
1 parent 2ba8380 commit da1ab3a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 82 deletions.
131 changes: 60 additions & 71 deletions lam/lib/treeview.inc
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ class TreeView {
/**
* @var array schema attributes
*/
private $schemaAttributes = null;
private $schemaAttributes;

/**
* @var array schema object classes
*/
private $schemaObjectClasses = null;
private $schemaObjectClasses;

/**
* Returns the JSON to answer an AJAX request.
Expand Down Expand Up @@ -132,7 +132,6 @@ class TreeView {
logNewMessage(LOG_ERR, 'Invalid command for tree view: ' . $command);
die;
}
return json_encode(array());
}

/**
Expand Down Expand Up @@ -645,7 +644,7 @@ class TreeView {
return $this->getAttributePasswordInputField($attributeName, $value, $required, $isSingleValue);
}
if ($this->isJpegAttribute($attributeName, $schemaAttribute)) {
return $this->getAttributeJpegInputField($attributeName, $value, $required, $index);
return $this->getAttributeJpegInputField($attributeName, $value, $index);
}
if (($schemaAttribute !== null) && $schemaAttribute->isBinary()) {
$value = base64_encode($value);
Expand Down Expand Up @@ -732,16 +731,15 @@ class TreeView {
}

/**
* Returns an input field for a JPG image attribute.
*
* @param string $attributeName attribute name
* @param string $value value
* @param bool $required required
* @param int $index index
* @return htmlElement input field
* @throws LAMException error writing image file
*/
private function getAttributeJpegInputField(string $attributeName, string $value, bool $required, int $index): htmlElement {
* Returns an input field for a JPG image attribute.
*
* @param string $attributeName attribute name
* @param string $value value
* @param int $index index
* @return htmlElement input field
* @throws LAMException error writing image file
*/
private function getAttributeJpegInputField(string $attributeName, string $value, int $index): htmlElement {
$tempFilesManager = new LamTemporaryFilesManager();
$fileName = $tempFilesManager->registerTemporaryFile('.jpg');
$handle = $tempFilesManager->openTemporaryFileForWrite($fileName);
Expand Down Expand Up @@ -846,46 +844,44 @@ class TreeView {
$changes = array_change_key_case($changes, CASE_LOWER);
logNewMessage(LOG_DEBUG, 'LDAP changes for ' . $dn . ': ' . print_r($changes, true));
$ldapChanges = array();
if (!empty($changes)) {
foreach ($changes as $attrName => $change) {
$schemaAttribute = isset($schemaAttributes[$attrName]) ? $schemaAttributes[$attrName] : null;
if (isset($change['new'])) {
$newValues = $change['new'];
if (isset($change['hash'])) {
$newValues = $this->applyPasswordHash($newValues, $change['hash']);
}
elseif (($schemaAttribute !== null) && $schemaAttribute->isBinary()) {
$newValues = $this->decodeBinaryAttributeValues($newValues);
}
$ldapChanges[$attrName] = $newValues;
foreach ($changes as $attrName => $change) {
$schemaAttribute = isset($schemaAttributes[$attrName]) ? $schemaAttributes[$attrName] : null;
if (isset($change['new'])) {
$newValues = $change['new'];
if (isset($change['hash'])) {
$newValues = $this->applyPasswordHash($newValues, $change['hash']);
}
if (isset($change['delete']) && isset($attributes[$attrName])) {
$oldValues = $attributes[$attrName];
$changed = false;
foreach ($change['delete'] as $index) {
if (isset($oldValues[$index])) {
unset($oldValues[$index]);
$changed = true;
}
}
if ($changed) {
if (($schemaAttribute !== null) && $schemaAttribute->isBinary()) {
$oldValues = $this->decodeBinaryAttributeValues($oldValues);
}
$ldapChanges[$attrName] = array_values($oldValues);
}
elseif (($schemaAttribute !== null) && $schemaAttribute->isBinary()) {
$newValues = $this->decodeBinaryAttributeValues($newValues);
}
if (!empty($change['upload'])) {
if (empty($ldapChanges[$attrName]) && !empty($attributes[$attrName])) {
$ldapChanges[$attrName] = $attributes[$attrName];
}
$oldValues = !empty($attributes[$attrName]) ? $attributes[$attrName] : array();
if ($this->isMultiValueAttribute($oldValues, $schemaAttribute)) {
$ldapChanges[$attrName][] = base64_decode($change['upload']);
$ldapChanges[$attrName] = $newValues;
}
if (isset($change['delete']) && isset($attributes[$attrName])) {
$oldValues = $attributes[$attrName];
$changed = false;
foreach ($change['delete'] as $index) {
if (isset($oldValues[$index])) {
unset($oldValues[$index]);
$changed = true;
}
else {
$ldapChanges[$attrName][0] = base64_decode($change['upload']);
}
if ($changed) {
if (($schemaAttribute !== null) && $schemaAttribute->isBinary()) {
$oldValues = $this->decodeBinaryAttributeValues($oldValues);
}
$ldapChanges[$attrName] = array_values($oldValues);
}
}
if (!empty($change['upload'])) {
if (empty($ldapChanges[$attrName]) && !empty($attributes[$attrName])) {
$ldapChanges[$attrName] = $attributes[$attrName];
}
$oldValues = !empty($attributes[$attrName]) ? $attributes[$attrName] : array();
if ($this->isMultiValueAttribute($oldValues, $schemaAttribute)) {
$ldapChanges[$attrName][] = base64_decode($change['upload']);
}
else {
$ldapChanges[$attrName][0] = base64_decode($change['upload']);
}
}
}
Expand Down Expand Up @@ -1226,14 +1222,11 @@ class TreeView {
if ($attributeNames === null) {
$attributeNames = array();
}
if (!empty($objectClassObject->getSupClasses())) {
foreach ($objectClassObject->getSupClasses() as $superClass) {
$attributeNames = array_merge($attributeNames, $this->getMustAttributeNamesRecursive($objectClasses, $superClass));
}
foreach ($objectClassObject->getSupClasses() as $superClass) {
$attributeNames = array_merge($attributeNames, $this->getMustAttributeNamesRecursive($objectClasses, $superClass));
}
$attributeNames = array_map('strtolower', $attributeNames);
$attributeNames = array_unique($attributeNames);
return $attributeNames;
return array_unique($attributeNames);
}

/**
Expand All @@ -1250,24 +1243,20 @@ class TreeView {
if ($attributeNames === null) {
$attributeNames = array();
}
if (!empty($objectClassObject->getSupClasses())) {
foreach ($objectClassObject->getSupClasses() as $superClass) {
$attributeNames = array_merge($attributeNames, $this->getMayAttributeNamesRecursive($objectClasses, $superClass));
}
foreach ($objectClassObject->getSupClasses() as $superClass) {
$attributeNames = array_merge($attributeNames, $this->getMayAttributeNamesRecursive($objectClasses, $superClass));
}
$attributeNames = array_map('strtolower', $attributeNames);
$attributeNames = array_unique($attributeNames);
return $attributeNames;
return array_unique($attributeNames);
}

/**
* Returns the content for the check attributes step of node creation.
*
* @param string $dn DN
* @param string|null $errorMessage error if any
* @return string JSON data
*/
private function createNewNodeCheckAttributesStep(string $dn, string $errorMessage = null): string {
* Returns the content for the check attributes step of node creation.
*
* @param string $dn DN
* @return string JSON data
*/
private function createNewNodeCheckAttributesStep(string $dn): string {
$objectClasses = $_POST['objectClasses'];
$rdnAttribute = $_POST['rdn'];
$attributeChanges = json_decode($_POST['attributes'], true);
Expand Down Expand Up @@ -1556,8 +1545,8 @@ class TreeView {
/**
* Performs paste operations.
*
* @param string $dn
* @return string
* @param string $dn DN to paste
* @return string JSON data
*/
private function paste(string $dn): string {
$targetDn = base64_decode($_POST['targetDn']);
Expand Down
12 changes: 6 additions & 6 deletions lam/lib/types.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace LAM\TYPES;
/*
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
Copyright (C) 2005 - 2022 Roland Gruber
Copyright (C) 2005 - 2023 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -103,15 +103,15 @@ class ConfiguredType {

private $id;

private $suffix = null;
private $suffix;

private $attributes = null;
private $attributes;

private $alias = null;
private $alias;

private $additionalLdapFilter = null;
private $additionalLdapFilter;

private $hidden = null;
private $hidden;

private $baseType;

Expand Down
6 changes: 3 additions & 3 deletions lam/lib/upload.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ include_once('pdf.inc');
*/
class Uploader {

private $accounts = null;
private $data = null;
private $type = null;
private $accounts;
private $data;
private $type;
private $endTime;

const TIME_LIMIT = 10;
Expand Down
3 changes: 1 addition & 2 deletions lam/lib/webauthn.inc
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,9 @@ class PublicKeyCredentialSourceRepositorySQLite extends PublicKeyCredentialSourc
* {@inheritDoc}
*/
protected function getPDO(): PDO {
$pdo = new PDO($this->getPdoUrl(), null, null, array(
return new PDO($this->getPdoUrl(), null, null, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
return $pdo;
}

/**
Expand Down

0 comments on commit da1ab3a

Please sign in to comment.