Skip to content

Commit

Permalink
[ENG-1037] - Users can have multi groups, more test & misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Phara0h committed Nov 4, 2019
1 parent d4ce2d8 commit cec0448
Show file tree
Hide file tree
Showing 21 changed files with 6,516 additions and 4,330 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -48,6 +48,7 @@ A blazing fast dynamic route level groups/permissions api gateway.
- [TRAVELLING_PORTAL_ICON](#travelling_portal_icon)
- [Proxy](#proxy)
- [TRAVELLING_PROXY_TIMEOUT](#travelling_proxy_timeout)
- [TRAVELLING_PROXY_SEND_TRAVELLING_HEADERS](#travelling_proxy_send_travelling_headers)
- [Redis](#redis)
- [TRAVELLING_REDIS_ENABLE](#travelling_redis_enable)
- [TRAVELLING_REDIS_URL](#travelling_redis_url)
Expand Down Expand Up @@ -311,6 +312,19 @@ ___
##### TRAVELLING_PROXY_TIMEOUT
*How long in seconds the proxy should wait on a request to finish. `0` is Infinity* </br>
> **Default**: `0`
##### TRAVELLING_PROXY_SEND_TRAVELLING_HEADERS
*Allows Travelling to send permission/user/group based headers along with the proxy route* </br>
> **Default**: `false`
Header | Description
----------|-------------
`un` | User's Username.
`uid` | User's Id.
`gn` | User's Group's name that allowed the request.
`gt `| User's Group's type that allowed the request.
`perm` | Permission's name that allowed the request.

___

### Redis
Expand Down
9 changes: 2 additions & 7 deletions include/database/index.js
Expand Up @@ -51,7 +51,6 @@ class Database {
if (user.password == await crypto.hash(password, null, user.getEncryptedProfile(user))) {
user.failed_login_attempts = 0;
await user.save();
user.addProperty('group', await Group.findById(user.group_id));

return {user, err: null};
}
Expand Down Expand Up @@ -82,13 +81,13 @@ class Database {

}

static async createAccount(username, password, email, group_id, group_request = null) {
static async createAccount(username, password, email, group_ids, group_request = null) {
var userProp = {
username,
password,
email,
group_request,
group_id,
group_ids,
change_username: false,
change_password: false,
created_on: Date.now(),
Expand Down Expand Up @@ -161,10 +160,6 @@ class Database {
return true;
}

static async getDefaultGroup() {
return await Group.getDefaultGroup();
}

static async initGroups(router) {

var grps = await Group.findAll();
Expand Down
84 changes: 75 additions & 9 deletions include/database/models/user.js
Expand Up @@ -20,7 +20,7 @@ class User extends Base(BaseModel, 'users', {
change_password: null,
reset_password: null,
email_verify: null,
group_id: null,
group_ids: null,
email: PGTypes.AutoCrypt,
created_on: null,
last_login: null,
Expand All @@ -44,7 +44,7 @@ class User extends Base(BaseModel, 'users', {
locked_reason text,
locked boolean DEFAULT false,
last_login json,
group_id UUID,
group_ids UUID[],
failed_login_attempts int DEFAULT 0,
change_username boolean DEFAULT false,
change_password boolean DEFAULT false,
Expand All @@ -55,21 +55,87 @@ class User extends Base(BaseModel, 'users', {
user_data bytea,
__user_data character varying(258),
eprofile character varying(350),
PRIMARY KEY (id, group_id)
PRIMARY KEY (id)
);`);

}

async resolveGroup() {
var group = await gm.getGroup(this.group_id) || await Group.findById(this.group_id);
var groups = [];
var groupsNames = [];
var groupsTypes = [];

if (!this.group) {
this.addProperty('group', group);
} else {
this.group = group;
if (!this.groups) {
this.addProperty('groups', groups);
}

return this;
// groups.name = '';
// groups.type = '';

for (var i = 0; i < this.group_ids.length; i++) {
const group = await gm.getGroup(this.group_ids[i]) || await Group.findById(this.group_ids[i]);

groups.push(group);
groupsNames.push(group.name);
groupsTypes.push(group.type);
// groups.name += group.name;
// if (this.group_ids.length < i + 1) {
// groups.name += '|';
// }
//
// groups.type += group.type;
// if (this.group_ids.length < i + 1) {
// groups.type += '|';
// }
}

this.groups = groups;

return {names: groupsNames, types: groupsTypes};
}

async addGroup(group) {
if (this.groups_id.indexOf(group.id) > -1) {
return false;
}
this.groups_id.push(group.id);
this.groups_id = [...this.groups_id];

return await this.save();
}

async removeGroup(group) {
var found = this.groups_id.indexOf(group.id);

if (found == -1) {
return false;
}
this.groups_id.splice(found, 1);
this.groups_id = [...this.groups_id];

return await this.save();
}

hasGroupId(id) {
return this.group_ids.indexOf(id) > -1;
}

hasGroupName(name) {
for (var i = 0; i < this.groups.length; i++) {
if (this.groups[i].name === name) {
return true;
}
}
return false;
}

hasGroupType(type) {
for (var i = 0; i < this.groups.length; i++) {
if (this.groups[i].type === type) {
return true;
}
}
return false;
}

toJSON() {
Expand Down
13 changes: 6 additions & 7 deletions include/routes/v1/auth.js
Expand Up @@ -10,7 +10,7 @@ const TokenHandler = require('../../token');
const {checkValidUser} = require('../../utils/user');
const gm = require('../../server/groupmanager');

var login = async (user, req, res, router) => {
var login = async (user, req, res) => {

/**
@TODO add check to ip to see if they are differnt then email the user of possible
Expand All @@ -23,12 +23,12 @@ var login = async (user, req, res, router) => {

user.failed_login_attempts = 0;
await user.save();
user.resolveGroup(router);
const groupsData = await user.resolveGroup();

req.createSession(user.id, {user});
req.createSession(user.id, {user, groupsData});
res = await CookieToken.newTokenInCookie(user.username, user.password, req, res);

config.log.logger.info('User Logged in: ' + user.username + ' (' + user.group.name + ')' + ' | ' + req.ip);
config.log.logger.info('User Logged in: ' + user.username + ' (' + groupsData.names + ')' + ' | ' + req.ip);

if (req.cookies['trav:backurl']) {
var url = req.cookies['trav:backurl'].split('|');
Expand All @@ -51,7 +51,6 @@ var login = async (user, req, res, router) => {
};

module.exports = function(app, opts, done) {
const router = opts.router;
// if(config.cors.enable) {
// app.use((req,res,next)=> {
// res.setHeader('access-control-allow-credentials', true)
Expand Down Expand Up @@ -105,7 +104,7 @@ module.exports = function(app, opts, done) {
try {
var user = await Database.checkAuth(username, email, req.body.password);

await login(user.user, req, res, router);
await login(user.user, req, res);
} catch (e) {
res.code(400).send(e.err && e.err.type == 'locked' ? {type: e.err.type, msg: e.err.msg, email: e.email} : {
type: 'login-error',
Expand Down Expand Up @@ -156,7 +155,7 @@ module.exports = function(app, opts, done) {
}

var dGroup = await gm.defaultGroup();
var user = await Database.createAccount(username, password, email, dGroup.id, groupRequest);
var user = await Database.createAccount(username, password, email, [dGroup.id], groupRequest);

config.log.logger.info('New User Created: ' + user.username + ' | ' + req.connection);
res.code(200).send('Account Created');
Expand Down

0 comments on commit cec0448

Please sign in to comment.