aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/lib/Service
diff options
context:
space:
mode:
Diffstat (limited to 'apps/user_ldap/lib/Service')
-rw-r--r--apps/user_ldap/lib/Service/BirthdateParserService.php44
-rw-r--r--apps/user_ldap/lib/Service/UpdateGroupsService.php35
2 files changed, 50 insertions, 29 deletions
diff --git a/apps/user_ldap/lib/Service/BirthdateParserService.php b/apps/user_ldap/lib/Service/BirthdateParserService.php
new file mode 100644
index 00000000000..8234161b3d8
--- /dev/null
+++ b/apps/user_ldap/lib/Service/BirthdateParserService.php
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\User_LDAP\Service;
+
+use DateTimeImmutable;
+use Exception;
+use InvalidArgumentException;
+
+class BirthdateParserService {
+ /**
+ * Try to parse the birthdate from LDAP.
+ * Supports LDAP's generalized time syntax, YYYYMMDD and YYYY-MM-DD.
+ *
+ * @throws InvalidArgumentException If the format of then given date is unknown
+ */
+ public function parseBirthdate(string $value): DateTimeImmutable {
+ // Minimum LDAP generalized date is "1994121610Z" with 11 chars
+ // While maximum other format is "1994-12-16" with 10 chars
+ if (strlen($value) > strlen('YYYY-MM-DD')) {
+ // Probably LDAP generalized time syntax
+ $value = substr($value, 0, 8);
+ }
+
+ // Should be either YYYYMMDD or YYYY-MM-DD
+ if (!preg_match('/^(\d{8}|\d{4}-\d{2}-\d{2})$/', $value)) {
+ throw new InvalidArgumentException("Unknown date format: $value");
+ }
+
+ try {
+ return new DateTimeImmutable($value);
+ } catch (Exception $e) {
+ throw new InvalidArgumentException(
+ "Unknown date format: $value",
+ 0,
+ $e,
+ );
+ }
+ }
+}
diff --git a/apps/user_ldap/lib/Service/UpdateGroupsService.php b/apps/user_ldap/lib/Service/UpdateGroupsService.php
index 79cd809ff6a..94f2a7fd4a1 100644
--- a/apps/user_ldap/lib/Service/UpdateGroupsService.php
+++ b/apps/user_ldap/lib/Service/UpdateGroupsService.php
@@ -3,32 +3,9 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Bart Visscher <bartv@thisnet.nl>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Côme Chilliet <come.chilliet@nextcloud.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Robin McCorkell <robin@mccorkell.me.uk>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\User_LDAP\Service;
@@ -113,7 +90,7 @@ class UpdateGroupsService {
if ($e->getReason() !== Exception::REASON_DATABASE_OBJECT_NOT_FOUND) {
/* If reason is not found something else removed the membership, that’s fine */
$this->logger->error(
- __CLASS__ . ' - group {group} membership failed to be removed (user {user})',
+ self::class . ' - group {group} membership failed to be removed (user {user})',
[
'app' => 'user_ldap',
'user' => $removedUser,
@@ -144,7 +121,7 @@ class UpdateGroupsService {
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
/* If reason is unique constraint something else added the membership, that’s fine */
$this->logger->error(
- __CLASS__ . ' - group {group} membership failed to be added (user {user})',
+ self::class . ' - group {group} membership failed to be added (user {user})',
[
'app' => 'user_ldap',
'user' => $addedUser,
@@ -190,7 +167,7 @@ class UpdateGroupsService {
} catch (Exception $e) {
if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->logger->error(
- __CLASS__ . ' - group {group} membership failed to be added (user {user})',
+ self::class . ' - group {group} membership failed to be added (user {user})',
[
'app' => 'user_ldap',
'user' => $user,