2018-07-03 07:54:24 +02:00
|
|
|
import {ErrorCode} from './utils';
|
|
|
|
import {API_ERROR} from './constants';
|
|
|
|
|
|
|
|
export function allow_action(action) {
|
2018-07-15 00:30:47 +02:00
|
|
|
return function(user, pkg, callback) {
|
|
|
|
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-15 00:47:47 +02:00
|
|
|
authenticate(user, password, cb) {
|
2018-07-03 07:54:24 +02:00
|
|
|
cb(ErrorCode.getForbidden(API_ERROR.BAD_USERNAME_PASSWORD));
|
|
|
|
},
|
|
|
|
|
2018-07-15 00:47:47 +02:00
|
|
|
add_user(user, password, cb) {
|
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'),
|
|
|
|
};
|
|
|
|
}
|