Skip to content
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

feat: able to set email #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions lib/Controller/AdminGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Mail\IMailer;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
Expand All @@ -37,6 +38,7 @@ public function __construct(
protected LoggerInterface $logger,
protected IGroupManager $groupManager,
protected IUserManager $userManager,
protected IMailer $mailer,
protected ISubAdmin $subAdmin,
protected IAppManager $appManager,
protected IAppConfig $appConfig,
Expand All @@ -54,6 +56,7 @@ public function __construct(
*
* @param string $groupid ID of the group
* @param string $displayname Display name of the group
* @param string $email Email of admin
* @param string $quota Group quota in "human readable" format. Default value is 1Gb.
* @param list<string> $apps List of app ids to enable
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
Expand All @@ -67,13 +70,14 @@ public function __construct(
public function createAdminGroup(
string $groupid,
string $displayname = '',
string $email = '',
string $quota = '1Gb',
array $apps = [],
): DataResponse {
$group = $this->addGroup($groupid, $displayname);
$this->setGroupQuota($groupid, $quota);
$this->enableApps($apps, $groupid);
$user = $this->createUser($groupid, $displayname);
$user = $this->createUser($groupid, $displayname, $email);
$group->addUser($user);
$this->addSubAdmin($user, $group);
return new DataResponse();
Expand Down Expand Up @@ -125,7 +129,7 @@ private function addSubAdmin(IUser $user, IGroup $group): void {
$this->subAdmin->createSubAdmin($user, $group);
}

private function createUser($userId, $displayName): IUser {
private function createUser($userId, $displayName, $email): IUser {
$passwordEvent = new GenerateSecurePasswordEvent();
$this->eventDispatcher->dispatchTyped($passwordEvent);
$password = $passwordEvent->getPassword() ?? $this->secureRandom->generate(20);
Expand All @@ -140,6 +144,12 @@ private function createUser($userId, $displayName): IUser {
throw $e;
}
}
if ($email !== '') {
if (!$this->mailer->validateMailAddress($email)) {
throw new OCSException('Invalid email');
}
$user->setSystemEMailAddress($email);
}
return $user;
}

Expand Down Expand Up @@ -169,6 +179,9 @@ private function setGroupQuota(string $groupId, string $quota): void {
}

private function enableApps(array $appIds, string $groupId): void {
if (!$appIds) {
return;
}
$this->jobList->add(EnableAppsForGroup::class, [
'groupId' => $groupId,
'appIds' => $appIds,
Expand Down
5 changes: 5 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
"default": "",
"description": "Display name of the group"
},
"email": {
"type": "string",
"default": "",
"description": "Email of admin"
},
"quota": {
"type": "string",
"default": "1Gb",
Expand Down
5 changes: 5 additions & 0 deletions src/types/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export interface operations {
* @default
*/
displayname?: string;
/**
* @description Email of admin
* @default
*/
email?: string;
/**
* @description Group quota in "human readable" format. Default value is 1Gb.
* @default 1Gb
Expand Down