diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 556cee65007212addd1a90d3b9da6acea31c4e47..37f5fa2cc9df1f5364e9ae71ea185e6e57bd1769 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -5,6 +5,7 @@ ## Neue Features - Der Stud.IP-Cache ist nun kompatibel zu PSR-6. ([TIC #3701](https://gitlab.studip.de/studip/studip/-/issues/3701)) +- Das `User`-Model hat die Methode `hasPermissionLevel()` erhalten, um einfach abfragen zu können, ob eine Person einen bestimmten Berechtigungsstatus hat. ([Issue #3453](https://gitlab.studip.de/studip/studip/-/issues/3453)) ## Breaking changes diff --git a/lib/models/User.class.php b/lib/models/User.class.php index a9dbc14d4a3cd21fda004cb70305a42b78d2113a..4894031b7caaf8087be74e36b46df872990622d5 100644 --- a/lib/models/User.class.php +++ b/lib/models/User.class.php @@ -1622,4 +1622,29 @@ class User extends AuthUserMd5 implements Range, PrivacyObject, Studip\Calendar\ return ''; } } + + /* + * Returns whether the user has the given permission (for the given range). + * + * @param string $permission + * @param Range|null $for_range + * + * @return bool + */ + public function hasPermissionLevel(string $permission, ?Range $for_range = null): bool + { + if (func_num_args() === 1) { + return $GLOBALS['perm']->have_perm($permission, $this->id); + } + + if ($for_range === null) { + throw new Exception('No valid range given'); + } + + if ($for_range instanceof User) { + return $GLOBALS['perm']->have_profile_perm($permission, $for_range->id, $this->id); + } + + return $GLOBALS['perm']->have_studip_perm($permission, $for_range->id, $this->id); + } }