Skip to content

Commit

Permalink
Merge branch 'master' into FEATURE-632
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Nov 5, 2023
2 parents 5a06a0d + 6f5b7f2 commit 1813cb2
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 85 deletions.
12 changes: 0 additions & 12 deletions .scrutinizer.yml

This file was deleted.

3 changes: 0 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
<a href="https://github.com/DirectoryTree/LdapRecord/actions">
<img src="https://img.shields.io/github/actions/workflow/status/directorytree/ldaprecord/run-tests.yml?branch=master&style=flat-square">
</a>
<a href="https://scrutinizer-ci.com/g/DirectoryTree/LdapRecord/?branch=master">
<img src="https://img.shields.io/scrutinizer/g/DirectoryTree/LdapRecord/master.svg?style=flat-square"/>
</a>
<a href="https://packagist.org/packages/DirectoryTree/LdapRecord">
<img src="https://img.shields.io/packagist/dt/DirectoryTree/LdapRecord.svg?style=flat-square"/>
</a>
Expand Down
8 changes: 4 additions & 4 deletions src/DetailedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ class DetailedError
* Constructor.
*/
public function __construct(
protected ?int $errorCode,
protected ?string $errorMessage,
protected int $errorCode,
protected string $errorMessage,
protected ?string $diagnosticMessage
) {
}

/**
* Returns the LDAP error code.
*/
public function getErrorCode(): ?int
public function getErrorCode(): int
{
return $this->errorCode;
}

/**
* Returns the LDAP error message.
*/
public function getErrorMessage(): ?string
public function getErrorMessage(): string
{
return $this->errorMessage;
}
Expand Down
2 changes: 1 addition & 1 deletion src/LdapResultResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
*/
public function successful(): bool
{
return $this->errorCode === 0 && empty($this->errorMessage);
return $this->errorCode === 0;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Models/ActiveDirectory/Computer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace LdapRecord\Models\ActiveDirectory;

use LdapRecord\Models\ActiveDirectory\Concerns\HasAccountControl;
use LdapRecord\Models\ActiveDirectory\Concerns\HasPrimaryGroup;
use LdapRecord\Models\ActiveDirectory\Relations\HasOnePrimaryGroup;
use LdapRecord\Models\Relations\HasMany;
use LdapRecord\Models\Relations\HasOne;

class Computer extends Entry
{
use HasAccountControl;
use HasPrimaryGroup;

/**
Expand Down
46 changes: 46 additions & 0 deletions src/Models/ActiveDirectory/Concerns/HasAccountControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace LdapRecord\Models\ActiveDirectory\Concerns;

use LdapRecord\Models\Attributes\AccountControl;

trait HasAccountControl
{
/**
* Determine if the user's account is enabled.
*/
public function isEnabled(): bool
{
return ! $this->isDisabled();
}

/**
* Determine if the user's account is disabled.
*/
public function isDisabled(): bool
{
return $this->accountControl()->hasFlag(AccountControl::ACCOUNTDISABLE);
}

/**
* Get the user's account control.
*/
public function accountControl(): AccountControl
{
return new AccountControl(
$this->getFirstAttribute('userAccountControl')
);
}

/**
* Set the user's account control attribute.
*/
public function setUserAccountControlAttribute(mixed $value): void
{
if ($value instanceof AccountControl) {
$value = $value->getValue();
}

$this->attributes['useraccountcontrol'] = [(int) $value];
}
}
31 changes: 3 additions & 28 deletions src/Models/ActiveDirectory/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Carbon\Carbon;
use Illuminate\Contracts\Auth\Authenticatable;
use LdapRecord\Models\ActiveDirectory\Concerns\HasAccountControl;
use LdapRecord\Models\ActiveDirectory\Concerns\HasPrimaryGroup;
use LdapRecord\Models\ActiveDirectory\Scopes\RejectComputerObjectClass;
use LdapRecord\Models\Attributes\AccountControl;
use LdapRecord\Models\Concerns\CanAuthenticate;
use LdapRecord\Models\Concerns\HasPassword;
use LdapRecord\Models\Relations\HasMany;
Expand All @@ -18,6 +18,7 @@ class User extends Entry implements Authenticatable
use HasPassword;
use HasPrimaryGroup;
use CanAuthenticate;
use HasAccountControl;

/**
* The password's attribute name.
Expand Down Expand Up @@ -63,33 +64,7 @@ protected static function boot(): void
// class. This is needed due to computer objects containing all
// of the ActiveDirectory 'user' object classes. Without
// this scope, they would be included in results.
static::addGlobalScope(new RejectComputerObjectClass());
}

/**
* Determine if the user's account is enabled.
*/
public function isEnabled(): bool
{
return ! $this->isDisabled();
}

/**
* Determine if the user's account is disabled.
*/
public function isDisabled(): bool
{
return $this->accountControl()->hasFlag(AccountControl::ACCOUNTDISABLE);
}

/**
* Get the user's account control.
*/
public function accountControl(): AccountControl
{
return new AccountControl(
$this->getFirstAttribute('userAccountControl')
);
static::addGlobalScope(new RejectComputerObjectClass);
}

/**
Expand Down
63 changes: 61 additions & 2 deletions src/Models/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ trait HasAttributes
*/
protected array $original = [];

/**
* The models changed attributes.
*/
protected array $changes = [];

/**
* The models attributes.
*/
Expand Down Expand Up @@ -207,7 +212,7 @@ protected function encodeValue(string $value): string
return $value;
}

return utf8_encode($value);
return mb_convert_encoding($value, 'UTF-8', 'ISO-8859-1');
}

/**
Expand Down Expand Up @@ -257,6 +262,16 @@ public function syncOriginal(): static
return $this;
}

/**
* Sync the changed attributes.
*/
public function syncChanges(): static
{
$this->changes = $this->getDirty();

return $this;
}

/**
* Fills the entry with the supplied attributes.
*/
Expand Down Expand Up @@ -823,7 +838,7 @@ public function filterRawAttributes(array $attributes = [], array $keys = ['coun
*/
public function hasAttribute(int|string $key): bool
{
return [] !== ($this->attributes[$this->normalizeAttributeKey($key)] ?? []);
return ($this->attributes[$this->normalizeAttributeKey($key)] ?? []) !== [];
}

/**
Expand Down Expand Up @@ -869,6 +884,14 @@ public function getDirty(): array
return $dirty;
}

/**
* Get the attributes that have been changed since the model was last saved.
*/
public function getChanges(): array
{
return $this->changes;
}

/**
* Determine if the given attribute is dirty.
*/
Expand All @@ -877,6 +900,42 @@ public function isDirty(string $key): bool
return ! $this->originalIsEquivalent($key);
}

/**
* Determine if given attribute has remained the same.
*/
public function isClean(string $key): bool
{
return ! $this->isDirty($key);
}

/**
* Discard attribute changes and reset the attributes to their original state.
*/
public function discardChanges(): static
{
[$this->attributes, $this->changes] = [$this->original, []];

return $this;
}

/**
* Determine if the model or any of the given attribute(s) were changed when the model was last saved.
*/
public function wasChanged(array|string $attributes = null): bool
{
if (func_num_args() === 0) {
return count($this->changes) > 0;
}

foreach ((array) $attributes as $attribute) {
if (array_key_exists($attribute, $this->changes)) {
return true;
}
}

return false;
}

/**
* Get the accessors being appended to the models array form.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public function jsonSerialize(): array
protected function convertAttributesForJson(array $attributes = []): array
{
// If the model has a GUID set, we need to convert it to its
// string format, due to it being in binary. Otherwise
// string format, due to it being in binary. Otherwise,
// we will receive a JSON serialization exception.
if (isset($attributes[$this->guidKey])) {
$attributes[$this->guidKey] = [$this->getConvertedGuid(
Expand Down Expand Up @@ -914,6 +914,8 @@ protected function performUpdate(): void

$this->dispatch('updated');

$this->syncChanges();

$this->syncOriginal();
}

Expand Down Expand Up @@ -1054,7 +1056,7 @@ protected function deleteLeafNodes(): void
->in($this->dn)
->list()
->each(function (Model $model) {
$model->delete($recursive = true);
$model->delete(recursive: true);
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function setCache(Cache $cache = null): static
/**
* Returns a new Query Builder instance.
*/
public function newInstance(string $baseDn = null): static
public function newInstance(string $baseDn = null): Builder
{
return (new static($this->connection))->setDn(
is_null($baseDn) ? $this->getDn() : $baseDn
Expand All @@ -166,7 +166,7 @@ public function newInstance(string $baseDn = null): static
/**
* Returns a new nested Query Builder instance.
*/
public function newNestedInstance(Closure $closure = null): static
public function newNestedInstance(Closure $closure = null): Builder
{
$query = $this->newInstance()->nested();

Expand Down Expand Up @@ -1022,7 +1022,7 @@ public function whereNotContains(string $field, string $value): static
*/
public function whereIn(string $field, array $values): static
{
return $this->orFilter(function (self $query) use ($field, $values) {
return $this->orFilter(function (Builder $query) use ($field, $values) {
foreach ($values as $value) {
$query->whereEquals($field, $value);
}
Expand Down
6 changes: 1 addition & 5 deletions src/Query/Filter/ConditionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,10 @@ protected function extractComponents(string $filter): array
$components = Str::whenContains(
$filter,
$this->operators,
fn ($operator, $filter) => explode($this->operator = $operator, $filter),
fn ($operator, $filter) => explode($this->operator = $operator, $filter, 2),
fn ($filter) => throw new ParserException("Invalid query condition. No operator found in [$filter]"),
);

if (count($components) !== 2) {
throw new ParserException("Invalid query filter [$filter]");
}

return $components;
}
}
4 changes: 2 additions & 2 deletions src/Query/Model/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getModel(): Model
/**
* Get a new model query builder instance.
*/
public function newInstance(string $baseDn = null): static
public function newInstance(string $baseDn = null): BaseBuilder
{
return parent::newInstance($baseDn)->model($this->model);
}
Expand Down Expand Up @@ -236,7 +236,7 @@ public function findManyByAnr(array $values = [], array|string $columns = ['*'])
*/
protected function prepareAnrEquivalentQuery(string $value): static
{
return $this->orFilter(function (self $query) use ($value) {
return $this->orFilter(function (BaseBuilder $query) use ($value) {
foreach ($this->model->getAnrAttributes() as $attribute) {
$query->whereEquals($attribute, $value);
}
Expand Down
Loading

0 comments on commit 1813cb2

Please sign in to comment.