Commit f3b4d7c1 by Ali Arshad

Merge branch 'user-registration' into 'master'

user-model-changed

See merge request jibc/jibc-meteor-api!1
parents eb83816b fe514a05
...@@ -16,21 +16,26 @@ export const seedDB = async () => { ...@@ -16,21 +16,26 @@ export const seedDB = async () => {
// Roles // Roles
await Promise.all([ await Promise.all([
Roles.insert({ Roles.insert({
Title: 'Admin', title: 'Admin',
Permissions: allPermissions, permissions: allPermissions,
Description: '', description: '',
}).toPromise(), }).toPromise(),
Roles.insert({ Roles.insert({
Title: 'Blocked', title: 'User',
Permissions: [], permissions: [PERMISSIONS.CAN_LOGIN, PERMISSIONS.CAN_UPDATE_OWN_USER],
Description: '', description: '',
}).toPromise(), }).toPromise(),
Roles.insert({
title: 'Blocked',
permissions: [],
description: '',
}),
]); ]);
} else { } else {
// Update Roles Permissions // Update Roles Permissions
await Promise.all([ await Promise.all([
Roles Roles
.update({ Title: 'Admin' }, { $set: { Permissions: allPermissions } }) .update({ title: 'Admin' }, { $set: { permissions: allPermissions } })
.toPromise(), .toPromise(),
]); ]);
} }
...@@ -44,10 +49,8 @@ export const seedDB = async () => { ...@@ -44,10 +49,8 @@ export const seedDB = async () => {
password: 'admin', password: 'admin',
email: 'ali.arshad@vqode.com', email: 'ali.arshad@vqode.com',
profile: { profile: {
Role: Roles.findOne({ Title: 'Admin' })._id, // admin role id name: 'Admin User',
FirstName: 'Admin', role: Roles.findOne({ title: 'Admin' })._id, // admin role id
LastName: 'User',
Status: 'ACTIVE',
}, },
}); });
} }
......
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { seedDB } from './app.seeding'; import { seedDB } from './app.seeding';
import { PERMISSIONS } from './config'; import { PERMISSIONS } from './config';
import { User } from './models/user';
import { UtilsService } from './services/utils.service'; import { UtilsService } from './services/utils.service';
Meteor.startup(() => { Meteor.startup(() => {
...@@ -13,9 +12,10 @@ Meteor.startup(() => { ...@@ -13,9 +12,10 @@ Meteor.startup(() => {
// Validate Login Attempt // Validate Login Attempt
Accounts.validateLoginAttempt((data): boolean => { Accounts.validateLoginAttempt((data): boolean => {
const user: User = data.user; const user: Meteor.User = data.user;
return user && user.profile.Role && UtilsService.hasPermissionOfUser(user, PERMISSIONS.CAN_LOGIN); return user && user.profile.role
&& UtilsService.hasPermissionOfUser(user, PERMISSIONS.CAN_LOGIN);
}); });
// Changing url of reset password // Changing url of reset password
......
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { Roles } from '../collections/role'; import { Roles } from '../collections/role';
import { PERMISSIONS } from '../config'; import { PERMISSIONS } from '../config';
import { User } from '../models/user';
import { UtilsService } from '../services/utils.service'; import { UtilsService } from '../services/utils.service';
import { Query } from '../models/query'; import { Query } from '../models/query';
...@@ -15,70 +14,41 @@ Meteor.methods({ ...@@ -15,70 +14,41 @@ Meteor.methods({
}, },
registerUser(usr: any): string { registerUser(user: any): string {
const role = Roles.findOne({ Slug: usr.Role }); const defaultRole = Roles.findOne({ title: user.role });
if (!Accounts.findUserByEmail(user.email)) {
if (role) {
if (!Accounts.findUserByEmail(usr.Email)) { const userObj = {
const user: User = { email: user.email,
email: usr.Email, password: user.password,
password: usr.Password, username: user.username,
username: usr.Email, profile: {
profile: { name: user.name,
FirstName: usr.FirstName, role: defaultRole._id,
LastName: usr.LastName, },
Role: role._id, };
OriginalRole: role._id, return Accounts.createUser(userObj);
Status: usr.Status,
},
};
const createdUserId = Accounts.createUser(user);
return createdUserId;
}
throw new Meteor.Error(422, 'Email address already in use.');
} }
throw new Meteor.Error(403, 'Not Enough Permissions'); throw new Meteor.Error(422, 'Email address already in use.');
}, },
updateUser(user: User): any { updateUser(user: any): any {
if (!UtilsService.hasPermission(PERMISSIONS.CAN_UPDATE_OWN_USER)) { if (!UtilsService.hasPermission(PERMISSIONS.CAN_UPDATE_OWN_USER)) {
throw new Meteor.Error(403, 'Forbidden.'); throw new Meteor.Error(403, 'Forbidden.');
} }
if (user._id) { if (user._id) {
const updateObj = { $set: { profile: user.profile } }; if (user.password) {
if (user.profile.Email) { Accounts.setPassword(user._id, user.password);
updateObj.$set['emails.0.address'] = user.profile.Email;
} }
const updateObj = { $set: { profile: user.profile } };
return Meteor.users.update(user._id, updateObj); return Meteor.users.update(user._id, updateObj);
} }
}, },
disableUser(user: User): any {
if (UtilsService.hasPermission(PERMISSIONS.CAN_UPDATE_USER)) {
const disableRole = Roles.findOne({ Title: 'Blocked' });
const usr = Meteor.users.findOne(user._id);
usr.profile.Role = disableRole._id;
return Meteor.users.update(user._id, usr); checkUserByEmail(email: string): any {
}
throw new Meteor.Error(403, 'Not enough permissions');
},
checkUserByEmail(email: string): User {
return Accounts.findUserByEmail(email); return Accounts.findUserByEmail(email);
}, },
async enableUser(user: User): Promise<any> {
if (UtilsService.hasPermission(PERMISSIONS.CAN_UPDATE_USER)) {
const role = Roles.findOne({ Slug: user.profile.OriginalRole });
const usr = Meteor.users.findOne(user._id);
usr.profile.Role = role._id;
return Meteor.users.update(user._id, usr);
}
throw new Meteor.Error(403, 'Not enough permissions');
},
}); });
export interface Role { export interface Role {
_id?: string; _id?: string;
Title: string; title: string;
Permissions: string[]; permissions: string[];
Description: string; description: string;
} }
import { Role } from './role';
export interface User {
_id?: string;
emails?: Meteor.UserEmail[];
email?: string;
password?: string;
username?: string;
profile?: UserProfile;
Role?: any;
OldPassword?: string;
NewPassword?: string;
ConfirmNewPassword?: string;
}
export interface UserProfile {
FirstName?: string;
LastName?: string;
Role?: string;
OriginalRole?: string;
RoleTitle?: string;
RoleObj?: Role;
AccountActiveUntil?: Date;
Phone?: string;
Address?: string;
Email?: string;
Password?: string;
Status?: 'ACTIVE' | 'DISABLED';
}
...@@ -22,7 +22,7 @@ publishComposite('usersList', (filters = {}) => { ...@@ -22,7 +22,7 @@ publishComposite('usersList', (filters = {}) => {
children: [ children: [
{ {
find(user): any { find(user): any {
return Roles.find({ _id: user.profile.Role }); return Roles.find({ _id: user.profile.role });
}, },
}, },
], ],
......
...@@ -3,36 +3,35 @@ import AWS = require('aws-sdk'); ...@@ -3,36 +3,35 @@ import AWS = require('aws-sdk');
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { Roles } from '../collections/role'; import { Roles } from '../collections/role';
import { User } from '../models/user';
export class UtilsService { export class UtilsService {
static getLoggedInUserPermissions(): string[] { static getLoggedInUserPermissions(): string[] {
const usr: User = Meteor.user(); const usr: Meteor.User = Meteor.user();
const role = Roles.findOne(usr.profile.Role); const role = Roles.findOne(usr.profile.role);
return role.Permissions; return role.permissions;
} }
static hasPermission(permissions: string | string[]): boolean { static hasPermission(permissions: string | string[]): boolean {
const usr: User = Meteor.user(); const usr: Meteor.User = Meteor.user();
if (usr) { if (usr) {
const role = Roles.findOne(usr.profile.Role); const role = Roles.findOne(usr.profile.role);
if (typeof permissions === 'string') { if (typeof permissions === 'string') {
return role.Permissions.indexOf(permissions) !== -1; return role.permissions.indexOf(permissions) !== -1;
} }
return permissions.every(p => role.Permissions.indexOf(p) !== -1); return permissions.every(p => role.permissions.indexOf(p) !== -1);
} }
return false; return false;
} }
static hasPermissionOfUser(user: User, permission: string): boolean { static hasPermissionOfUser(user: Meteor.User, permission: string): boolean {
const role = Roles.findOne(user.profile.Role); const role = Roles.findOne(user.profile.role);
return role.Permissions.indexOf(permission) !== -1; return role.permissions.indexOf(permission) !== -1;
} }
static uploadToAWS(base64, key): Observable<any> { static uploadToAWS(base64, key): Observable<any> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment