-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e670fe
commit 9d87cf2
Showing
7 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const core = require('gls-core-service'); | ||
const BasicController = core.controllers.Basic; | ||
const Whitelist = require('../model/Whitelist'); | ||
|
||
class WhitelistController extends BasicController { | ||
constructor({ connector }) { | ||
super({ connector }); | ||
|
||
this._whitelistMap = new Map(); // username -> set of cids | ||
this._cidSet = new Set(); // set of cids | ||
} | ||
|
||
async _askRegService({ username }) { | ||
// TODO: implement call to service | ||
return true; | ||
} | ||
|
||
_addInMem({ username, channelId }) { | ||
this._cidSet.add(channelId); | ||
|
||
let userCids = this._whitelistMap.get(username); | ||
|
||
if (userCids) { | ||
userCids.add(channelId); | ||
} else { | ||
userCids = new Set(channelId); | ||
} | ||
|
||
this._whitelistMap.set(username, userCids); | ||
} | ||
|
||
_removeFromMem(username) { | ||
const cids = this._whitelistMap.get(username); | ||
if (cids) { | ||
cids.forEach(cid => { | ||
this._cidSet.delete(cid); | ||
}); | ||
} | ||
} | ||
|
||
async isAllowed({ channelId, username }) { | ||
// in memory -> allowed | ||
if (this._cidSet.has(channelId)) return true; | ||
|
||
const user = await Whitelist.findOne({ username }); | ||
|
||
// explicitly banned -> not allowed | ||
if (user && user.banned) { | ||
return false; | ||
} | ||
|
||
// in db -> allowed and should be stored in memory | ||
if (user && !user.banned) { | ||
this._addInMem({ username, channelId }); | ||
|
||
return true; | ||
} | ||
|
||
const inRegService = await this._askRegService({ username }); | ||
|
||
if (!inRegService) { | ||
return false; | ||
} | ||
|
||
// in reg service -> add to mongo and to in-mem | ||
await Whitelist.create({ username }); | ||
this._addInMem({ username, channelId }); | ||
|
||
return true; | ||
} | ||
|
||
async banUser(username) { | ||
await Whitelist.findOneAndRemove({ username }); | ||
|
||
this._removeFromMem(username); | ||
} | ||
} | ||
|
||
module.exports = WhitelistController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const core = require('gls-core-service'); | ||
const MongoDB = core.services.MongoDB; | ||
|
||
module.exports = MongoDB.makeModel('Whitelist', { | ||
username: { | ||
type: String, | ||
required: true, | ||
}, | ||
banned: { | ||
type: Boolean, | ||
required: true, | ||
default: false, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters