Skip to content

Commit

Permalink
Adding displayName to FederatedUser
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Jun 9, 2021
1 parent 0e6d1ab commit 177f001
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/IFederatedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function getUserId(): string;

public function getUserType(): int;

public function getDisplayName(): string;

public function getBasedOn(): ?Circle;

}
Expand Down
56 changes: 47 additions & 9 deletions lib/Model/FederatedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class FederatedUser extends ManagedModel implements
/** @var int */
private $userType;

/** @var string */
private $displayName = '';

/** @var Circle */
private $basedOn;

Expand Down Expand Up @@ -97,6 +100,7 @@ public function __construct() {
* @param string $userId
* @param string $instance
* @param int $type
* @param string $displayName
* @param Circle|null $basedOn
*
* @return $this
Expand All @@ -105,10 +109,12 @@ public function set(
string $userId,
string $instance = '',
int $type = Member::TYPE_USER,
string $displayName = '',
?Circle $basedOn = null
): self {

$this->userId = $userId;
$this->displayName = ($displayName === '') ? $userId : $displayName;
$this->setInstance($instance);
$this->userType = $type;
$this->basedOn = $basedOn;
Expand Down Expand Up @@ -166,14 +172,31 @@ public function setUserType(int $userType): self {
return $this;
}


/**
* @return int
*/
public function getUserType(): int {
return $this->userType;
}

/**
* @param string $displayName
*
* @return FederatedUser
*/
public function setDisplayName(string $displayName): self {
$this->displayName = $displayName;

return $this;
}

/**
* @return string
*/
public function getDisplayName(): string {
return $this->displayName;
}


/**
* @return bool
Expand Down Expand Up @@ -320,8 +343,9 @@ public function import(array $data): IDeserializable {
}

$this->setSingleId($this->get('id', $data));
$this->setUserId($this->get('user_id', $data));
$this->setUserType($this->getInt('user_type', $data));
$this->setUserId($this->get('userId', $data));
$this->setUserType($this->getInt('userType', $data));
$this->setDisplayName($this->get('displayName', $data));
$this->setInstance($this->get('instance', $data));
//$this->setMemberships($this->getArray('memberships'));

Expand All @@ -347,9 +371,21 @@ public function importFromCircle(Circle $circle): self {

if ($circle->isConfig(Circle::CFG_SINGLE)) {
$owner = $circle->getOwner();
$this->set($owner->getUserId(), $owner->getInstance(), $owner->getUserType(), $circle);
$this->set(
$owner->getUserId(),
$owner->getInstance(),
$owner->getUserType(),
$owner->getDisplayName(),
$circle
);
} else {
$this->set($circle->getDisplayName(), $circle->getInstance(), Member::TYPE_CIRCLE, $circle);
$this->set(
$circle->getDisplayName(),
$circle->getInstance(),
Member::TYPE_CIRCLE,
$circle->getDisplayName(),
$circle
);
}

return $this;
Expand All @@ -371,6 +407,7 @@ public function importFromDatabase(array $data, string $prefix = ''): INC22Query
$this->setSingleId($this->get($prefix . 'single_id', $data));
$this->setUserId($this->get($prefix . 'user_id', $data));
$this->setUserType($this->getInt($prefix . 'user_type', $data));
$this->setDisplayName($this->get($prefix . 'cached_name', $data));
$this->setInstance($this->get($prefix . 'instance', $data));

$this->getManager()->manageImportFromDatabase($this, $data, $prefix);
Expand All @@ -385,10 +422,11 @@ public function importFromDatabase(array $data, string $prefix = ''): INC22Query
*/
public function jsonSerialize(): array {
$arr = [
'id' => $this->getSingleId(),
'user_id' => $this->getUserId(),
'user_type' => $this->getUserType(),
'instance' => $this->getManager()->fixInstance($this->getInstance())
'id' => $this->getSingleId(),
'userId' => $this->getUserId(),
'userType' => $this->getUserType(),
'displayName' => $this->getDisplayName(),
'instance' => $this->getManager()->fixInstance($this->getInstance())
];

if ($this->hasBasedOn()) {
Expand Down
18 changes: 17 additions & 1 deletion lib/Service/FederatedUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public function getAppInitiator(string $appId, int $appNumber): FederatedUser {
$circle->setSource($appNumber);

$federatedUser = new FederatedUser();
$federatedUser->set($appId, '', Member::TYPE_APP, $circle);
$federatedUser->set($appId, '', Member::TYPE_APP, $appId, $circle);

$this->fillSingleCircleId($federatedUser);

Expand Down Expand Up @@ -715,6 +715,9 @@ public function getFederatedUser_Group(string $groupName, string $instance): Fed
* @param string $mailAddress
*
* @return FederatedUser
* @throws ContactAddressBookNotFoundException
* @throws ContactFormatException
* @throws ContactNotFoundException
* @throws FederatedUserException
* @throws InvalidIdException
* @throws RequestBuilderException
Expand All @@ -733,6 +736,9 @@ public function getFederatedUser_Mail(string $mailAddress): FederatedUser {
* @param string $contactPath
*
* @return FederatedUser
* @throws ContactAddressBookNotFoundException
* @throws ContactFormatException
* @throws ContactNotFoundException
* @throws FederatedUserException
* @throws InvalidIdException
* @throws RequestBuilderException
Expand Down Expand Up @@ -875,10 +881,20 @@ private function getSingleCircle(FederatedUser $federatedUser, bool $generate =
* @throws ContactNotFoundException
*/
private function getLocalDisplayName(FederatedUser $federatedUser): string {
if (!$this->configService->isLocalInstance($federatedUser->getInstance())) {
return $federatedUser->getUserId();
}

if ($federatedUser->getUserType() === Member::TYPE_CONTACT) {
return $this->contactService->getDisplayName($federatedUser->getUserId());
}

if ($federatedUser->getUserType() === Member::TYPE_USER) {
$user = $this->userManager->get($federatedUser->getUserId());

return $user->getDisplayName();
}

return $federatedUser->getUserId();
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Service/MemberService.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ function(FederatedUser $federatedUser) use ($patron) {
* @throws RemoteNotFoundException
* @throws RemoteResourceNotFoundException
* @throws UnknownRemoteException
* @throws RequestBuilderException
*/
public function removeMember(string $memberId): array {
$this->federatedUserService->mustHaveCurrentUser();
Expand Down Expand Up @@ -302,6 +303,7 @@ public function removeMember(string $memberId): array {
* @throws RemoteResourceNotFoundException
* @throws UnknownRemoteException
* @throws FederatedItemException
* @throws RequestBuilderException
*/
public function memberLevel(string $memberId, int $level): array {
$this->federatedUserService->mustHaveCurrentUser();
Expand Down

0 comments on commit 177f001

Please sign in to comment.