Compare commits
4 Commits
739295b162
...
99184b9568
Author | SHA1 | Date |
---|---|---|
b1ek | 99184b9568 | |
b1ek | 45d276c5af | |
b1ek | 04f961e25d | |
b1ek | 24598c2068 |
|
@ -1,17 +1,82 @@
|
||||||
const { Model, DataTypes } = require('sequelize');
|
const { Model, DataTypes } = require('sequelize');
|
||||||
const { sequelize } = require('.');
|
const { sequelize } = require('.');
|
||||||
|
|
||||||
class Permission extends Model {
|
/** @type {{data: Permission[], updated: number, expired: function}} */
|
||||||
|
let cached = {
|
||||||
|
data: undefined,
|
||||||
|
updated: 0,
|
||||||
|
expired: () => {
|
||||||
|
return (Date.now() - updated) >= 1000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Permission extends Model {
|
||||||
|
static async cache() {
|
||||||
|
if (!cached.expired()) return;
|
||||||
|
|
||||||
|
cached.data = await Permission.findAll();
|
||||||
|
cached.updated = Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value from cache
|
||||||
|
* @param {number} user
|
||||||
|
* @param {string} permission
|
||||||
|
* @returns {Permission?}
|
||||||
|
*/
|
||||||
|
static async findFromCache(user, permission) {
|
||||||
|
await this.cache();
|
||||||
|
for (const perm of cached.data) {
|
||||||
|
if (perm.user == userid && perm.permission == permission)
|
||||||
|
return perm;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a permission for user
|
||||||
|
* @param {number} user
|
||||||
|
* @param {string} permission
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
static async forUser(user, permission) {
|
||||||
|
await this.cache();
|
||||||
|
const data = await this.findFromCache(user, permission);
|
||||||
|
if (!data) return false;
|
||||||
|
return data.value != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a permission for user
|
||||||
|
* @param {number} user
|
||||||
|
* @param {string} permission
|
||||||
|
* @param {number} value
|
||||||
|
* @returns {Permission}
|
||||||
|
*/
|
||||||
|
static async set(user, permission, value) {
|
||||||
|
const existing = await Permission.findOne({where: {user, permission}});
|
||||||
|
|
||||||
|
let perm;
|
||||||
|
|
||||||
|
if (!existing) perm = await Permission.create({user, permission, value});
|
||||||
|
else perm = await Permission.update({user, permission, value}, {where: {user, permission}});
|
||||||
|
await this.cache();
|
||||||
|
|
||||||
|
return perm;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async userAllowed(user, permission) {
|
||||||
|
return this.forUser(user, permission);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Permission.structure = {
|
Permission.structure = {
|
||||||
// id: {
|
id: {
|
||||||
// type: DataTypes.BIGINT,
|
type: DataTypes.BIGINT,
|
||||||
// primaryKey: true,
|
primaryKey: true,
|
||||||
// autoIncrement: true,
|
autoIncrement: true,
|
||||||
// allowNull: false
|
allowNull: false
|
||||||
// },
|
},
|
||||||
user: {
|
user: {
|
||||||
type: DataTypes.BIGINT,
|
type: DataTypes.BIGINT,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"broadcast": true, "data": {"inline": "blek! Announcement is EOL and will be shut down at 2nd July 2023. Please update your app to the latest version to stop recieiving these notifications."}}
|
|
@ -31,8 +31,32 @@ html(style='overflow-y:auto' lang='en_US')
|
||||||
//- UX
|
//- UX
|
||||||
meta(name='viewport' content='width=device-width, initial-scale=1')
|
meta(name='viewport' content='width=device-width, initial-scale=1')
|
||||||
|
|
||||||
|
if (process.env.APP_DEBUG == 'true')
|
||||||
|
style.
|
||||||
|
.debug_warning {
|
||||||
|
margin:0;padding:0;
|
||||||
|
color:darkred;
|
||||||
|
font-weight:bold;
|
||||||
|
font-size:16pt;
|
||||||
|
position:fixed;
|
||||||
|
top:50%;
|
||||||
|
left:50%;
|
||||||
|
transform:translate(-50%,-50%);
|
||||||
|
text-align:center;
|
||||||
|
pointer-events:none;
|
||||||
|
opacity:0.4;
|
||||||
|
text-shadow:0 2px 1px #a07060a0
|
||||||
|
}
|
||||||
|
|
||||||
block head
|
block head
|
||||||
body(style='overflow-y:auto')
|
body(style='overflow-y:auto')
|
||||||
|
if (process.env.APP_DEBUG == 'true')
|
||||||
|
p(class='debug_warning')
|
||||||
|
| Warning: THE WEBSITE IS IN DEBUG MODE!
|
||||||
|
br
|
||||||
|
| If the site is not currently running locally, please
|
||||||
|
br
|
||||||
|
| contact the maintainers and notify them of the issue.
|
||||||
table(width='100%' height='100%' class='body_table')
|
table(width='100%' height='100%' class='body_table')
|
||||||
tr
|
tr
|
||||||
td(class='side_menu')
|
td(class='side_menu')
|
||||||
|
|
Loading…
Reference in New Issue