You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
4 years ago
|
import { isNull } from 'lodash';
|
||
|
import { PERM, PRIV } from '../model/builtin';
|
||
5 years ago
|
|
||
|
global.Hydro.ui.nav = [];
|
||
|
|
||
4 years ago
|
const trueChecker = () => true;
|
||
4 years ago
|
const Checker = (perm: string, priv: number, checker: Function = trueChecker) => (handler) => (
|
||
4 years ago
|
checker(handler)
|
||
|
&& (perm ? handler.user.hasPerm(perm) : true)
|
||
|
&& (priv ? handler.user.hasPriv(priv) : true)
|
||
|
);
|
||
|
|
||
4 years ago
|
const Item = (
|
||
|
name: string, args: any, prefix: string,
|
||
|
...permPrivChecker: Array<number | string | Function | Array<number | string>>
|
||
|
) => {
|
||
|
let _priv: number;
|
||
|
let _perm: string;
|
||
|
let checker: Function = trueChecker;
|
||
4 years ago
|
for (const item of permPrivChecker) {
|
||
4 years ago
|
if (item instanceof Object && !isNull(item)) {
|
||
|
if (item instanceof Array) {
|
||
|
if (typeof item[0] === 'number') {
|
||
|
// @ts-ignore
|
||
|
_priv = item;
|
||
|
} else if (typeof item[0] === 'string') {
|
||
|
// @ts-ignore
|
||
|
_perm = item;
|
||
|
}
|
||
|
} else if (typeof item.call !== 'undefined') {
|
||
4 years ago
|
checker = item;
|
||
|
}
|
||
|
} else if (typeof item === 'number') {
|
||
|
_priv = item;
|
||
|
} else if (typeof item === 'string') {
|
||
|
_perm = item;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
checker = Checker(_perm, _priv, checker);
|
||
5 years ago
|
global.Hydro.ui.nav.push({
|
||
4 years ago
|
name, args: args || {}, prefix, checker,
|
||
5 years ago
|
});
|
||
|
};
|
||
|
|
||
4 years ago
|
Item('homepage', null, 'homepage');
|
||
4 years ago
|
Item('problem_main', null, 'problem', PERM.PERM_VIEW_PROBLEM);
|
||
|
Item('training_main', null, 'training', PERM.PERM_VIEW_TRAINING);
|
||
|
Item('homework_main', null, 'homework', PERM.PERM_VIEW_HOMEWORK);
|
||
|
Item('discussion_main', null, 'discussion', PERM.PERM_VIEW_DISCUSSION);
|
||
|
Item('contest_main', null, 'contest', PERM.PERM_VIEW_CONTEST);
|
||
4 years ago
|
Item('record_main', null, 'record');
|
||
4 years ago
|
Item('ranking', null, 'ranking', PERM.PERM_VIEW_RANKING);
|
||
4 years ago
|
Item('domain_dashboard', null, 'domain', PERM.PERM_EDIT_DOMAIN);
|
||
4 years ago
|
Item('manage_dashboard', null, 'manage', PRIV.PRIV_EDIT_SYSTEM);
|
||
5 years ago
|
|
||
4 years ago
|
global.Hydro.lib.nav = Item;
|
||
|
export default Item;
|