2626 */
2727namespace OCA \Provisioning_API \Controller ;
2828
29+ use OC \AppFramework \Middleware \Security \Exceptions \NotAdminException ;
2930use OCP \AppFramework \Http ;
3031use OCP \AppFramework \Http \DataResponse ;
3132use OCP \AppFramework \OCSController ;
3233use OCP \IAppConfig ;
3334use OCP \IConfig ;
35+ use OCP \IGroupManager ;
36+ use OCP \IL10N ;
3437use OCP \IRequest ;
38+ use OCP \IUser ;
39+ use OCP \IUserSession ;
40+ use OCP \Settings \IDelegatedSettings ;
41+ use OCP \Settings \IManager ;
3542
3643class AppConfigController extends OCSController {
3744
@@ -41,6 +48,18 @@ class AppConfigController extends OCSController {
4148 /** @var IAppConfig */
4249 protected $ appConfig ;
4350
51+ /** @var IUserSession */
52+ private $ userSession ;
53+
54+ /** @var IL10N */
55+ private $ l10n ;
56+
57+ /** @var IGroupManager */
58+ private $ groupManager ;
59+
60+ /** @var IManager */
61+ private $ settingManager ;
62+
4463 /**
4564 * @param string $appName
4665 * @param IRequest $request
@@ -50,10 +69,18 @@ class AppConfigController extends OCSController {
5069 public function __construct (string $ appName ,
5170 IRequest $ request ,
5271 IConfig $ config ,
53- IAppConfig $ appConfig ) {
72+ IAppConfig $ appConfig ,
73+ IUserSession $ userSession ,
74+ IL10N $ l10n ,
75+ IGroupManager $ groupManager ,
76+ IManager $ settingManager ) {
5477 parent ::__construct ($ appName , $ request );
5578 $ this ->config = $ config ;
5679 $ this ->appConfig = $ appConfig ;
80+ $ this ->userSession = $ userSession ;
81+ $ this ->l10n = $ l10n ;
82+ $ this ->groupManager = $ groupManager ;
83+ $ this ->settingManager = $ settingManager ;
5784 }
5885
5986 /**
@@ -99,12 +126,23 @@ public function getValue(string $app, string $key, string $defaultValue = ''): D
99126
100127 /**
101128 * @PasswordConfirmationRequired
129+ * @NoSubAdminRequired
130+ * @NoAdminRequired
102131 * @param string $app
103132 * @param string $key
104133 * @param string $value
105134 * @return DataResponse
106135 */
107136 public function setValue (string $ app , string $ key , string $ value ): DataResponse {
137+ $ user = $ this ->userSession ->getUser ();
138+ if ($ user === null ) {
139+ throw new \Exception ("User is not logged in. " ); // Should not happen, since method is guarded by middleware
140+ }
141+
142+ if (!$ this ->isAllowedToChangedKey ($ user , $ app , $ key )) {
143+ throw new NotAdminException ($ this ->l10n ->t ('Logged in user must be an admin or have authorization to edit this setting. ' ));
144+ }
145+
108146 try {
109147 $ this ->verifyAppId ($ app );
110148 $ this ->verifyConfigKey ($ app , $ key , $ value );
@@ -170,4 +208,30 @@ protected function verifyConfigKey(string $app, string $key, string $value) {
170208 throw new \InvalidArgumentException ('The given key can not be set, unlimited quota is forbidden on this instance ' );
171209 }
172210 }
211+
212+ private function isAllowedToChangedKey (IUser $ user , string $ app , string $ key ): bool {
213+ // Admin right verification
214+ $ isAdmin = $ this ->groupManager ->isAdmin ($ user ->getUID ());
215+ if ($ isAdmin ) {
216+ return true ;
217+ }
218+
219+ $ settings = $ this ->settingManager ->getAllAllowedAdminSettings ($ user );
220+ foreach ($ settings as $ setting ) {
221+ if (!($ setting instanceof IDelegatedSettings)) {
222+ continue ;
223+ }
224+ $ allowedKeys = $ setting ->getAuthorizedAppConfig ();
225+ if (!array_key_exists ($ app , $ allowedKeys )) {
226+ continue ;
227+ }
228+ foreach ($ allowedKeys [$ app ] as $ regex ) {
229+ if ($ regex === $ key
230+ || (str_starts_with ($ regex , '/ ' ) && preg_match ($ regex , $ key ) === 1 )) {
231+ return true ;
232+ }
233+ }
234+ }
235+ return false ;
236+ }
173237}
0 commit comments