fdsafs
This commit is contained in:
@@ -35,6 +35,100 @@ export const getEnableAnonymousAccess = () => {
|
||||
return true;
|
||||
};
|
||||
|
||||
export const DEFAULT_ANON_PERMISSIONS = Object.freeze({
|
||||
upload: false,
|
||||
comment: true,
|
||||
comment_attachments: false,
|
||||
comment_vote: true,
|
||||
poll_vote: true,
|
||||
tag: true,
|
||||
tag_vote: true,
|
||||
favorite: true,
|
||||
rate_item: false,
|
||||
filter: true,
|
||||
exclude_tags: true,
|
||||
anonymize_users: false,
|
||||
allowed_modes: ['sfw', 'nsfw', 'untagged', 'all', 'nsfl'],
|
||||
allowed_mimes: ['image', 'video', 'audio', 'flash', 'pdf']
|
||||
});
|
||||
|
||||
export const getAnonPermissions = () => {
|
||||
const fromConfig = cfg.anonymous_permissions || cfg.websrv?.anonymous_permissions || {};
|
||||
return {
|
||||
...DEFAULT_ANON_PERMISSIONS,
|
||||
...fromConfig
|
||||
};
|
||||
};
|
||||
|
||||
export const getAnonAnonymize = () => {
|
||||
const perms = getAnonPermissions();
|
||||
if (typeof perms.anonymize_users === 'boolean') return perms.anonymize_users;
|
||||
if (typeof perms.anon_anonymize === 'boolean') return perms.anon_anonymize;
|
||||
if (cfg.main && typeof cfg.main.anon_anonymize === 'boolean') return cfg.main.anon_anonymize;
|
||||
if (cfg.main && typeof cfg.main.anonymous_anonymize === 'boolean') return cfg.main.anonymous_anonymize;
|
||||
if (typeof cfg.anon_anonymize === 'boolean') return cfg.anon_anonymize;
|
||||
if (typeof cfg.anonymous_anonymize === 'boolean') return cfg.anonymous_anonymize;
|
||||
return false;
|
||||
};
|
||||
|
||||
export const isAnonymizeSession = (session) => {
|
||||
if (!session || typeof session !== 'object' || !session.user) {
|
||||
return !!(cfg.main?.guest_anonymize ?? cfg.guest_anonymize);
|
||||
}
|
||||
if (session.is_anon || session.user === 'anonymous' || (typeof session.user === 'string' && session.user.startsWith('anon_'))) {
|
||||
return getAnonAnonymize();
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
export const canAnonDo = (action) => {
|
||||
if (!getEnableAnonymousAccess()) return false;
|
||||
const perms = getAnonPermissions();
|
||||
if (action === 'exclude_tags' || action === 'exclude_tag' || action === 'tag_exclude') {
|
||||
if (perms.exclude_tags !== undefined) return !!perms.exclude_tags;
|
||||
if (perms.exclude_tag !== undefined) return !!perms.exclude_tag;
|
||||
if (perms.tag_exclude !== undefined) return !!perms.tag_exclude;
|
||||
return perms.filter !== undefined ? !!perms.filter : true;
|
||||
}
|
||||
return perms[action] !== undefined ? !!perms[action] : !!DEFAULT_ANON_PERMISSIONS[action];
|
||||
};
|
||||
|
||||
export const getAnonAllowedModes = () => {
|
||||
const perms = getAnonPermissions();
|
||||
return Array.isArray(perms.allowed_modes) ? perms.allowed_modes.map(m => String(m).toLowerCase()) : DEFAULT_ANON_PERMISSIONS.allowed_modes;
|
||||
};
|
||||
|
||||
export const getAnonAllowedMimes = () => {
|
||||
const perms = getAnonPermissions();
|
||||
return Array.isArray(perms.allowed_mimes) ? perms.allowed_mimes.map(m => String(m).toLowerCase()) : DEFAULT_ANON_PERMISSIONS.allowed_mimes;
|
||||
};
|
||||
|
||||
export const canAnonMode = (mode) => {
|
||||
if (!canAnonDo('filter')) return false;
|
||||
const allowed = getAnonAllowedModes();
|
||||
const modeNames = ['sfw', 'nsfw', 'untagged', 'all', 'nsfl'];
|
||||
const name = typeof mode === 'number' ? modeNames[mode] : String(mode).toLowerCase();
|
||||
return allowed.includes(name);
|
||||
};
|
||||
|
||||
export const canAnonMime = (mime) => {
|
||||
if (!canAnonDo('filter')) return false;
|
||||
const allowed = getAnonAllowedMimes();
|
||||
return allowed.includes(String(mime).toLowerCase());
|
||||
};
|
||||
|
||||
export const isAnonSession = (session) => {
|
||||
if (!session) return true;
|
||||
if (session === true) return false;
|
||||
if (typeof session !== 'object' || !session.user) return true;
|
||||
return !!(session.is_anon || session.user === 'anonymous' || (typeof session.user === 'string' && session.user.startsWith('anon_')));
|
||||
};
|
||||
|
||||
export const checkAnonPermission = (session, action) => {
|
||||
if (!isAnonSession(session)) return true;
|
||||
return canAnonDo(action);
|
||||
};
|
||||
|
||||
export const ensureAllItemsHaveSlugs = async () => {
|
||||
try {
|
||||
const rows = await db`SELECT id FROM items WHERE slug IS NULL OR slug = ''`;
|
||||
|
||||
Reference in New Issue
Block a user