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 () => {
// Roles
await Promise.all([
Roles.insert({
Title: 'Admin',
Permissions: allPermissions,
Description: '',
title: 'Admin',
permissions: allPermissions,
description: '',
}).toPromise(),
Roles.insert({
Title: 'Blocked',
Permissions: [],
Description: '',
title: 'User',
permissions: [PERMISSIONS.CAN_LOGIN, PERMISSIONS.CAN_UPDATE_OWN_USER],
description: '',
}).toPromise(),
Roles.insert({
title: 'Blocked',
permissions: [],
description: '',
}),
]);
} else {
// Update Roles Permissions
await Promise.all([
Roles
.update({ Title: 'Admin' }, { $set: { Permissions: allPermissions } })
.update({ title: 'Admin' }, { $set: { permissions: allPermissions } })
.toPromise(),
]);
}
......@@ -44,10 +49,8 @@ export const seedDB = async () => {
password: 'admin',
email: 'ali.arshad@vqode.com',
profile: {
Role: Roles.findOne({ Title: 'Admin' })._id, // admin role id
FirstName: 'Admin',
LastName: 'User',
Status: 'ACTIVE',
name: 'Admin User',
role: Roles.findOne({ title: 'Admin' })._id, // admin role id
},
});
}
......
import { Meteor } from 'meteor/meteor';
import { seedDB } from './app.seeding';
import { PERMISSIONS } from './config';
import { User } from './models/user';
import { UtilsService } from './services/utils.service';
Meteor.startup(() => {
......@@ -13,9 +12,10 @@ Meteor.startup(() => {
// Validate Login Attempt
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
......
import { Meteor } from 'meteor/meteor';
import { Roles } from '../collections/role';
import { PERMISSIONS } from '../config';
import { User } from '../models/user';
import { UtilsService } from '../services/utils.service';
import { Query } from '../models/query';
......@@ -15,70 +14,41 @@ Meteor.methods({
},
registerUser(usr: any): string {
const role = Roles.findOne({ Slug: usr.Role });
registerUser(user: any): string {
const defaultRole = Roles.findOne({ title: user.role });
if (!Accounts.findUserByEmail(user.email)) {
if (role) {
if (!Accounts.findUserByEmail(usr.Email)) {
const user: User = {
email: usr.Email,
password: usr.Password,
username: usr.Email,
const userObj = {
email: user.email,
password: user.password,
username: user.username,
profile: {
FirstName: usr.FirstName,
LastName: usr.LastName,
Role: role._id,
OriginalRole: role._id,
Status: usr.Status,
name: user.name,
role: defaultRole._id,
},
};
const createdUserId = Accounts.createUser(user);
return createdUserId;
return Accounts.createUser(userObj);
}
throw new Meteor.Error(422, 'Email address already in use.');
}
throw new Meteor.Error(403, 'Not Enough Permissions');
},
updateUser(user: User): any {
updateUser(user: any): any {
if (!UtilsService.hasPermission(PERMISSIONS.CAN_UPDATE_OWN_USER)) {
throw new Meteor.Error(403, 'Forbidden.');
}
if (user._id) {
const updateObj = { $set: { profile: user.profile } };
if (user.profile.Email) {
updateObj.$set['emails.0.address'] = user.profile.Email;
if (user.password) {
Accounts.setPassword(user._id, user.password);
}
const updateObj = { $set: { profile: user.profile } };
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);
}
throw new Meteor.Error(403, 'Not enough permissions');
},
checkUserByEmail(email: string): User {
checkUserByEmail(email: string): any {
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 {
_id?: string;
Title: string;
Permissions: string[];
Description: string;
title: string;
permissions: 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 = {}) => {
children: [
{
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');
import { Meteor } from 'meteor/meteor';
import { Observable, Subject } from 'rxjs';
import { Roles } from '../collections/role';
import { User } from '../models/user';
export class UtilsService {
static getLoggedInUserPermissions(): string[] {
const usr: User = Meteor.user();
const role = Roles.findOne(usr.profile.Role);
const usr: Meteor.User = Meteor.user();
const role = Roles.findOne(usr.profile.role);
return role.Permissions;
return role.permissions;
}
static hasPermission(permissions: string | string[]): boolean {
const usr: User = Meteor.user();
const usr: Meteor.User = Meteor.user();
if (usr) {
const role = Roles.findOne(usr.profile.Role);
const role = Roles.findOne(usr.profile.role);
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;
}
static hasPermissionOfUser(user: User, permission: string): boolean {
const role = Roles.findOne(user.profile.Role);
static hasPermissionOfUser(user: Meteor.User, permission: string): boolean {
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> {
......
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