2018-07-17 20:33:51 +02:00
|
|
|
// @flow
|
|
|
|
|
2018-07-03 07:54:24 +02:00
|
|
|
import {ErrorCode} from './utils';
|
|
|
|
import {API_ERROR} from './constants';
|
|
|
|
|
2018-07-17 20:33:51 +02:00
|
|
|
import type {RemoteUser, Package, Callback} from '@verdaccio/types';
|
|
|
|
|
|
|
|
export function allow_action(action: string) {
|
|
|
|
return function(user: RemoteUser, pkg: Package, callback: Callback) {
|
2018-07-15 00:30:47 +02:00
|
|
|
const {name, groups} = user;
|
|
|
|
const hasPermission = pkg[action].some((group) => name === group || groups.includes(group));
|
2018-07-03 07:54:24 +02:00
|
|
|
|
2018-07-15 00:30:47 +02:00
|
|
|
if (hasPermission) {
|
|
|
|
return callback(null, true);
|
2018-07-03 07:54:24 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 00:30:47 +02:00
|
|
|
if (name) {
|
|
|
|
callback(ErrorCode.getForbidden(`user ${name} is not allowed to ${action} package ${pkg.name}`));
|
2018-07-03 07:54:24 +02:00
|
|
|
} else {
|
2018-07-15 00:30:47 +02:00
|
|
|
callback(ErrorCode.getForbidden(`unregistered users are not allowed to ${action} package ${pkg.name}`));
|
2018-07-03 07:54:24 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDefaultPlugins() {
|
|
|
|
return {
|
2018-07-17 20:33:51 +02:00
|
|
|
authenticate(user: string, password: string, cb: Callback) {
|
2018-07-03 07:54:24 +02:00
|
|
|
cb(ErrorCode.getForbidden(API_ERROR.BAD_USERNAME_PASSWORD));
|
|
|
|
},
|
|
|
|
|
2018-07-17 20:33:51 +02:00
|
|
|
add_user(user: string, password: string, cb: Callback) {
|
2018-07-03 07:54:24 +02:00
|
|
|
return cb(ErrorCode.getConflict(API_ERROR.BAD_USERNAME_PASSWORD));
|
|
|
|
},
|
|
|
|
|
|
|
|
allow_access: allow_action('access'),
|
|
|
|
allow_publish: allow_action('publish'),
|
|
|
|
};
|
|
|
|
}
|