Skip to content

Commit ac6ccd0

Browse files
Merge pull request #41 from ercanucan/refactor/allow_publish
refactor(gitlab): simplify allow_publish function
2 parents 323ef0b + dbe36e3 commit ac6ccd0

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

src/authcache.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export class AuthCache {
4242
sha.update(JSON.stringify({ username: username, password: password }));
4343
return sha.digest('hex');
4444
}
45-
4645
}
4746

4847
export type UserDataGroups = {

src/gitlab.js

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default class VerdaccioGitLab implements IPluginAuth {
158158
}
159159

160160
allow_access(user: RemoteUser, _package: VerdaccioGitlabPackageAccess, cb: Callback) {
161-
if (!_package.gitlab) { return cb(); }
161+
if (!_package.gitlab) return cb();
162162

163163
if ((_package.access || []).includes('$authenticated') && user.name !== undefined) {
164164
this.logger.debug(`[gitlab] allow user: ${user.name} access to package: ${_package.name}`);
@@ -170,14 +170,12 @@ export default class VerdaccioGitLab implements IPluginAuth {
170170
this.logger.debug(`[gitlab] deny user: ${user.name || ''} access to package: ${_package.name}`);
171171
return cb(null, false);
172172
}
173-
174173
}
175174

176175
allow_publish(user: RemoteUser, _package: VerdaccioGitlabPackageAccess, cb: Callback) {
177-
if (!_package.gitlab) { return cb(); }
176+
if (!_package.gitlab) return cb();
178177
let packageScopePermit = false;
179178
let packagePermit = false;
180-
181179
// Only allow to publish packages when:
182180
// - the package has exactly the same name as one of the user groups, or
183181
// - the package scope is the same as one of the user groups
@@ -186,31 +184,20 @@ export default class VerdaccioGitLab implements IPluginAuth {
186184
if (real_group === _package.name) {
187185
packagePermit = true;
188186
break;
189-
} else {
190-
if (_package.name.indexOf('@') === 0) {
191-
if (real_group === _package.name.slice(1, _package.name.lastIndexOf('/'))) {
192-
packageScopePermit = true;
193-
break;
194-
}
195-
}
187+
} else if (_package.name.indexOf('@') === 0 && real_group === _package.name.slice(1, _package.name.lastIndexOf('/'))) {
188+
packageScopePermit = true;
189+
break;
196190
}
197191
}
198192

199-
if (packagePermit === true) {
200-
this.logger.debug(`[gitlab] user: ${user.name || ''} allowed to publish package: ${_package.name} based on package-name`);
193+
if (packagePermit || packageScopePermit) {
194+
const perm = packagePermit ? 'package-name' : 'package-scope';
195+
this.logger.debug(`[gitlab] user: ${user.name || ''} allowed to publish package: ${_package.name} based on ${perm}`);
201196
return cb(null, false);
202197
} else {
203-
if (packageScopePermit === true) {
204-
this.logger.debug(`[gitlab] user: ${user.name || ''} allowed to publish package: ${_package.name} based on package-scope`);
205-
return cb(null, false);
206-
} else {
207-
this.logger.debug(`[gitlab] user: ${user.name || ''} denied from publishing package: ${_package.name}`);
208-
if (_package.name.indexOf('@') === 0) {
209-
return cb(httperror[403](`must have required permissions: ${this.config.publish || ''} at package-scope`));
210-
} else {
211-
return cb(httperror[403](`must have required permissions: ${this.config.publish || ''} at package-name`));
212-
}
213-
}
198+
this.logger.debug(`[gitlab] user: ${user.name || ''} denied from publishing package: ${_package.name}`);
199+
const missingPerm = _package.name.indexOf('@') === 0 ? 'package-scope' : 'package-name';
200+
return cb(httperror[403](`must have required permissions: ${this.config.publish || ''} at ${missingPerm}`));
214201
}
215202
}
216203

@@ -237,5 +224,4 @@ export default class VerdaccioGitLab implements IPluginAuth {
237224
}
238225
});
239226
}
240-
241227
}

0 commit comments

Comments
 (0)