Skip to content

Commit 512425e

Browse files
JJ-8JJ-8
authored andcommitted
Add past ctfs role
This role is between guest and member and allows a member to access past ctfs. Access is denied for upcoming CTFs or active CTFs.
1 parent f74486b commit 512425e

7 files changed

+187
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var dbm;
4+
var type;
5+
var seed;
6+
var fs = require('fs');
7+
var path = require('path');
8+
var Promise;
9+
10+
/**
11+
* We receive the dbmigrate dependency from dbmigrate initially.
12+
* This enables us to not have to rely on NODE_PATH.
13+
*/
14+
exports.setup = function(options, seedLink) {
15+
dbm = options.dbmigrate;
16+
type = dbm.dataType;
17+
seed = seedLink;
18+
Promise = options.Promise;
19+
};
20+
21+
exports.up = function(db) {
22+
var filePath = path.join(__dirname, 'sqls', '20210701075424-past-ctf-role-up.sql');
23+
return new Promise( function( resolve, reject ) {
24+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
25+
if (err) return reject(err);
26+
console.log('received data: ' + data);
27+
28+
resolve(data);
29+
});
30+
})
31+
.then(function(data) {
32+
return db.runSql(data);
33+
});
34+
};
35+
36+
exports.down = function(db) {
37+
var filePath = path.join(__dirname, 'sqls', '20210701075424-past-ctf-role-down.sql');
38+
return new Promise( function( resolve, reject ) {
39+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
40+
if (err) return reject(err);
41+
console.log('received data: ' + data);
42+
43+
resolve(data);
44+
});
45+
})
46+
.then(function(data) {
47+
return db.runSql(data);
48+
});
49+
};
50+
51+
exports._meta = {
52+
"version": 1
53+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
var dbm;
4+
var type;
5+
var seed;
6+
var fs = require('fs');
7+
var path = require('path');
8+
var Promise;
9+
10+
/**
11+
* We receive the dbmigrate dependency from dbmigrate initially.
12+
* This enables us to not have to rely on NODE_PATH.
13+
*/
14+
exports.setup = function(options, seedLink) {
15+
dbm = options.dbmigrate;
16+
type = dbm.dataType;
17+
seed = seedLink;
18+
Promise = options.Promise;
19+
};
20+
21+
exports.up = function(db) {
22+
var filePath = path.join(__dirname, 'sqls', '20210701081957-past-ctf-functions-up.sql');
23+
return new Promise( function( resolve, reject ) {
24+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
25+
if (err) return reject(err);
26+
console.log('received data: ' + data);
27+
28+
resolve(data);
29+
});
30+
})
31+
.then(function(data) {
32+
return db.runSql(data);
33+
});
34+
};
35+
36+
exports.down = function(db) {
37+
var filePath = path.join(__dirname, 'sqls', '20210701081957-past-ctf-functions-down.sql');
38+
return new Promise( function( resolve, reject ) {
39+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
40+
if (err) return reject(err);
41+
console.log('received data: ' + data);
42+
43+
resolve(data);
44+
});
45+
})
46+
.then(function(data) {
47+
return db.runSql(data);
48+
});
49+
};
50+
51+
exports._meta = {
52+
"version": 1
53+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
GRANT user_guest TO user_member;
2+
3+
REVOKE user_guest FROM user_past_ctfs;
4+
5+
REVOKE user_past_ctfs FROM user_member;
6+
7+
UPDATE ctfnote_private.user SET role = 'user_guest'::ctfnote.role WHERE role = 'user_past_ctfs'::ctfnote.role;
8+
9+
UPDATE ctfnote_private.invitation_link SET role = 'user_guest'::ctfnote.role WHERE role = 'user_past_ctfs'::ctfnote.role;
10+
11+
DROP ROLE user_past_ctfs;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE ROLE user_past_ctfs;
2+
3+
REVOKE user_guest FROM user_member;
4+
5+
GRANT user_guest TO user_past_ctfs;
6+
7+
GRANT user_past_ctfs TO user_member;
8+
9+
ALTER TYPE ctfnote.role ADD VALUE IF NOT EXISTS 'user_past_ctfs';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CREATE OR REPLACE FUNCTION ctfnote_private.can_play_ctf (ctf_id int)
2+
RETURNS boolean
3+
AS $$
4+
SELECT
5+
ctfnote_private.is_member ()
6+
OR (
7+
SELECT
8+
TRUE
9+
FROM
10+
ctfnote.invitation
11+
WHERE
12+
invitation.ctf_id = can_play_ctf.ctf_id
13+
AND invitation.profile_id = ctfnote_private.current_id ())
14+
$$
15+
LANGUAGE sql
16+
STABLE;
17+
18+
GRANT EXECUTE ON FUNCTION ctfnote_private.can_play_ctf TO user_guest;
19+
20+
DROP FUNCTION ctfnote_private.is_past_ctfs ();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--Check if current user is allowed to access past ctfs
2+
CREATE FUNCTION ctfnote_private.is_past_ctfs ()
3+
RETURNS boolean
4+
AS $$
5+
SELECT
6+
ctfnote_private.current_role () = 'user_past_ctfs'::ctfnote.role
7+
OR ctfnote_private.is_member ();
8+
9+
$$
10+
LANGUAGE sql
11+
STABLE;
12+
13+
GRANT EXECUTE ON FUNCTION ctfnote_private.is_past_ctfs () TO user_anonymous;
14+
15+
CREATE OR REPLACE FUNCTION ctfnote_private.can_play_ctf (ctf_id int)
16+
RETURNS boolean
17+
AS $$
18+
SELECT
19+
ctfnote_private.is_member ()
20+
OR (
21+
SELECT
22+
TRUE
23+
FROM
24+
ctfnote.invitation
25+
WHERE
26+
invitation.ctf_id = can_play_ctf.ctf_id
27+
AND invitation.profile_id = ctfnote_private.current_id ())
28+
OR (
29+
ctfnote_private.is_past_ctfs ()
30+
AND
31+
(SELECT TRUE FROM ctfnote.ctf WHERE end_time < NOW() AND ctf.id = can_play_ctf.ctf_id)
32+
)
33+
$$
34+
LANGUAGE sql
35+
STABLE;
36+
37+
GRANT EXECUTE ON FUNCTION ctfnote_private.can_play_ctf TO user_guest;

front/src/boot/CTFNote.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class CTFNote {
55
constructor() {
66
this.roles = {
77
USER_GUEST: 1,
8+
USER_PAST_CTFS: 5,
89
USER_MEMBER: 2,
910
USER_MANAGER: 3,
1011
USER_ADMIN: 4
@@ -80,6 +81,9 @@ class CTFNote {
8081
get isGuest() {
8182
return this.me.roleId >= this.roles.USER_GUEST;
8283
}
84+
get isPastCtfs() {
85+
return this.me.roleId >= this.roles.USER_PAST_CTFS;
86+
}
8387
get isMember() {
8488
return this.me.roleId >= this.roles.USER_MEMBER;
8589
}

0 commit comments

Comments
 (0)