@@ -124,8 +124,7 @@ public function lock(LockContext $lockScope): FileLock {
124
124
125
125
// Extend lock expiry if matching
126
126
if (
127
- $ known ->getType () === $ lockScope ->getType () &&
128
- $ known ->getOwner () === $ lockScope ->getOwner ()
127
+ $ known ->getType () === $ lockScope ->getType () && $ known ->getOwner () === $ lockScope ->getOwner ()
129
128
) {
130
129
$ known ->setTimeout (
131
130
$ known ->getTimeout () - $ known ->getETA () + $ this ->configService ->getTimeoutSeconds ()
@@ -150,6 +149,10 @@ public function lock(LockContext $lockScope): FileLock {
150
149
}
151
150
}
152
151
152
+ public function update (FileLock $ lock ) {
153
+ $ this ->locksRequest ->update ($ lock );
154
+ }
155
+
153
156
public function getAppName (string $ appId ): ?string {
154
157
$ appInfo = $ this ->appManager ->getAppInfo ($ appId );
155
158
return $ appInfo ['name ' ] ?? null ;
@@ -165,10 +168,8 @@ public function unlock(LockContext $lock, bool $force = false): FileLock {
165
168
$ this ->notice ('unlocking file ' , false , ['fileLock ' => $ lock ]);
166
169
167
170
$ known = $ this ->getLockFromFileId ($ lock ->getNode ()->getId ());
168
- if (!$ force && ($ lock ->getOwner () !== $ known ->getOwner () || $ lock ->getType () !== $ known ->getType ())) {
169
- throw new UnauthorizedUnlockException (
170
- $ this ->l10n ->t ('File can only be unlocked by the owner of the lock ' )
171
- );
171
+ if (!$ force ) {
172
+ $ this ->canUnlock ($ lock , $ known );
172
173
}
173
174
174
175
$ this ->locksRequest ->delete ($ known );
@@ -177,6 +178,33 @@ public function unlock(LockContext $lock, bool $force = false): FileLock {
177
178
return $ known ;
178
179
}
179
180
181
+ public function canUnlock (LockContext $ request , FileLock $ current ): void {
182
+ $ isSameUser = $ current ->getOwner () === $ this ->userId ;
183
+ $ isSameToken = $ request ->getOwner () === $ current ->getToken ();
184
+ $ isSameOwner = $ request ->getOwner () === $ current ->getOwner ();
185
+ $ isSameType = $ request ->getType () === $ current ->getType ();
186
+
187
+ // Check the token for token based locks
188
+ if ($ request ->getType () === ILock::TYPE_TOKEN ) {
189
+ if ($ isSameToken || $ isSameUser ) {
190
+ return ;
191
+ }
192
+
193
+ throw new UnauthorizedUnlockException (
194
+ $ this ->l10n ->t ('File can only be unlocked by providing a valid owner lock token ' )
195
+ );
196
+ }
197
+
198
+ // Otherwise, we check if the owner (user id OR app id) for a match
199
+ if ($ isSameOwner && $ isSameType ) {
200
+ return ;
201
+ }
202
+
203
+ throw new UnauthorizedUnlockException (
204
+ $ this ->l10n ->t ('File can only be unlocked by the owner of the lock ' )
205
+ );
206
+ }
207
+
180
208
181
209
/**
182
210
* @throws InvalidPathException
@@ -254,7 +282,8 @@ public function injectMetadata(FileLock $lock): FileLock {
254
282
$ displayName = $ this ->getAppName ($ lock ->getOwner ()) ?? null ;
255
283
}
256
284
if ($ lock ->getType () === ILock::TYPE_TOKEN ) {
257
- $ displayName = $ lock ->getOwner ();
285
+ $ user = $ this ->userManager ->get ($ lock ->getOwner ());
286
+ $ displayName = $ user ? $ user ->getDisplayName (): $ lock ->getDisplayName ();
258
287
}
259
288
260
289
$ lock ->setDisplayName ($ displayName );
0 commit comments