strongloop loopback find users role using loaded operation hook - authorization

I have cloned a https://github.com/beeman/loopback-angular-admin and I have created a couple of new roles using the loopback explorer and I am trying to get all the roles of a particular user when he logs in by loaded operation hook defined here -
https://docs.strongloop.com/display/public/LB/Operation+hooks#Operationhooks-loaded
like this -
user.observe('loaded', function appendRole(ctx, next){
if(ctx.instance){
user.findOne({
where: {
id: ctx.instance.id
},
include: {
"relation":"roles"
}
})
}
next();
})
so how do I return roles of a particular user using operation hooks. I am able to get all the roles of a user using loopback explorer using this api call
http://localhost:80/api/users/567ce48d6503f9404b56bb3e/roles?access_token=gyPzW3rpr3uzve2bUHtZQWv8iV5PfZYW7QLicCs4GwIKTdNA33SeRAlgPIQef7AE
UPDATE :
So, I tried adding the following code to the user.js -
user.observe('loaded', function appendRole(ctx, next){
if(ctx.instance){
console.log(ctx.instance.roles);
}
next();
})
and I am getting the following output in the console -
{ [Function]
_receiver:
{ username: 'harshitladdha93#gmail.com',
password: '$2a$10$Bubhaq1LXFyCUn.W1/pEOewLSqspcP2GQlONwGH98V4HqCOAc9522',
email: 'harshitladdha93#gmail.com',
status: 'created',
created: Mon Jan 04 2016 22:53:53 GMT+0530 (IST),
firstName: 'Harshit',
lastName: 'Laddha',
gender: 'male',
birthday: '1993-07-30T18:30:00.000Z',
qualification: 'sa;',
experience: 'askjdl',
achievements: 'sakldj',
street: 'has',
locality: 'alskjd',
area: 'lkjd',
city: 'bangalore',
id: 568aaaa997ace4670b5d9ac2 },
_scope:
{ where: { principalId: 568aaaa997ace4670b5d9ac2 },
collect: 'role',
include: 'role' },
_targetClass: 'Role',
getAsync: [Function],
build: [Function: build],
create: [Function],
updateAll: [Function: updateAll],
destroyAll: [Function: destroyAll],
findById: [Function],
findOne: [Function: findOne],
count: [Function: count],
destroy: [Function],
updateById: [Function],
exists: [Function],
add: [Function],
remove: [Function] }
so how do I get the roles of user using loaded operation hook

Did you try ctx.instance.roles?
UPDATE: Ah crap, that's right, I guess you'll need to do a lookup, but you can't use a user.find() since it will trigger an infinite loop on the loaded observe. Try something like this:
user.observe('loaded', function getRoleMappings(ctx, next) {
var roleMapFilter = {
where: {principalId: ctx.instance.id},
include: ['role']
};
user.app.models.RoleMapping.find(roleMapFilter, function(err, roleMaps) {
if (err) {
console.log('roleMap', err);
next(err);
}
console.log("roleMaps ", roleMaps);
next();
});
});
console.log("roleMaps ", roleMaps); should output something like:
roleMaps [
{
id: 1,
principalType: 'USER',
principalId: '1',
roleId: 1,
role: {
id: 1,
name: 'admin',
description: null,
created: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT),
modified: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT)
}
},
{
id: 2,
principalType: 'USER',
principalId: '1',
roleId: 6,
role: {
id: 6,
name: 'sales',
description: null,
created: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT),
modified: Thu Jun 25 2015 10:24:40 GMT-0700 (PDT)
}
}
]

Related

How to do Prisma runtime model validation?

In my application, I have validated the input credential at the DTO level by using class-validator. But I need runtime model validation like sequelize ORM.
In sequelize:
'use strict';
import { DataTypes, Sequelize } from 'sequelize';
function User(sequelize: Sequelize) {
const user = sequelize.define(
'User',
{
name: {
type: DataTypes.STRING,
allowNull: false
},
role: {
type: DataTypes.STRING(20),
allowNull: false
},
email: {
type: new DataTypes.STRING,
allowNull: false,
validate: {
isEmail: {
// args: true,
msg: 'Invalid email'
},
len: {
args: [1, 100] as readonly [number, number],
msg: 'Email length should be 1 to 100 characters'
},
notNull: {
// args: true,
msg: 'Email cannot be empty'
}
}
},
password: {
type: DataTypes.VIRTUAL,
allowNull: true,
},
},
{
tableName: 'users',
underscored: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
paranoid: true
}
);
return user;
}
export default User;
Is there any possibility to do model validation in Prisma?
There is an open feature request for Prisma to support runtime model validation directly at the Schema level. Alternatively, you can leverage the Client Extensions to perform validation. There is an example in this blog post that shows how to perform custom runtime validation.

I am getting error when creating a storage for variables

My code -
export default Users = [
{
id: 1,
email: "user1#email.com",
username: "user1",
password: "password",
userToken: "token123",
},
{
id: 2,
email: "user2#email.com",
username: "user2",
password: "pass1234",
userToken: "token12345",
},
{
id: 3,
email: "testuser#email.com",
username: "testuser",
password: "testpass",
userToken: "testtoken",
},
];
Error -
ReferenceError: Users is not defined
Module.D:\Code\WhiteHatJr\Apps\unused_seller\model\users.js
D:/Code/WhiteHatJr/Apps/unused_seller/model/users.js:1
> 1 | export default Users = [
2 | {
3 | id: 1,
4 | email: "user1#email.com",
View compiled
I am making an app using react native, expo.
But I am facing this error. Don't know why.
But it seems like there is a problem with the variable I am using for storage.
try like this
const Users = [
{
id: 1,
email: "user1#email.com",
username: "user1",
password: "password",
userToken: "token123",
},
{
id: 2,
email: "user2#email.com",
username: "user2",
password: "pass1234",
userToken: "token12345",
},
{
id: 3,
email: "testuser#email.com",
username: "testuser",
password: "testpass",
userToken: "testtoken",
},
];
export default Users
See, you don't actually need that name (Users) here, as it won't be used anyway when module is imported. Quoting the doc:
Named exports are useful to export several values. During the import,
it is mandatory to use the same name of the corresponding object.
[...] But a default export can be imported with any name, for example:
// file test.js
let k; export default k = 12;
// some other file
import m from './test';
// note that we have the freedom to use import m instead of import k,
// because k was default export
console.log(m); // will log 12
As you don't need the name, you can make this quite concise:
export default [
{
id: 1,
email: "user1#email.com",
username: "user1",
password: "password",
userToken: "token123",
},
// ...
];
But if you actually need this data to be properly labeled, consider dropping default altogether and going the named exports road instead. In some teams, default exports are considered harmful.

How to create element associated with an existing object

I created the following two models...
const Account = sequelize.define("account",
{
id_account: {
type: Sequelize.INTEGER.UNSIGNED,
primaryKey: true,
autoIncrement: true
},
name: {...},
surname: {...},
username: {...},
password: {...}
},
{
name: {
singular: "Account",
plural: "Accounts"
},
freezeTableName: true,
hooks: {
beforeSave: ((account, options) => {
return bcrypt.hash(account.password, 10)
.then(hash => {account.password = hash;})
.catch(err => {throw new Error();});
})
}
});
const Genre = sequelize.define("genre",
{
genre_name: {
type: Sequelize.STRING(40),
primaryKey: true
},
url_img: {
type: Sequelize.STRING(40),
allowNull: false
}
},
{
name: {
singular: "Genre",
plural: "Genres"
},
freezeTableName: true
});
...and the following associations
Account.Genres = Account.belongsToMany(Genre, {
through: "AccountGenre",
foreignKey: "ref_account"
});
Genre.Accounts = Genere.belongsToMany(Account, {
through: "AccountGenre",
foreignKey: "ref_genre"
});
I created the following genres: Rock, Metal, Pop, Hardcore.
Now i want to create an Account and associate it 3 genres.
The following code creates the Account but doesn't create the association with the existing genres in the AccountGenre table:
const genresArray = ["Rock", "Metal", "Pop"];
const account = {...} // I have an object with account properties
Account.create({
name: account.name,
surname: account.cognome,
username: account.nome_utente,
password: account.password,
genres: genresArray
}, {
include: [Genre]
});
What's wrong in this code?
The problem here is that you gave the reference to the
association the plural name “Genres”. You need the same in the include statement.
include: [Genres]

LokiJS: insert existing value for index doesn't error - how to make unique indices?

if I try to override an existing indexed field, I do not get an error.
It should error, because it is not update()!
var loki = require('lokijs');
var db = new loki('test.json');
var users = db.addCollection('users', { indices: ['userID']});
users.insert(
{
'name': 'Anna',
'userID': 1
},
{
'name': 'Bernd',
'userID': 2
},
{
'name': 'Christa',
'userID': 3
});
db.save();
users.insert({'name': 'Dieter','userID': 2}); // this should error!!
How can I make an unique index to get an error when trying to inset an existing userID ?
the indices option creates an index on the field, which allows for faster retrieval because the index lives in a separate sorted array within the collection (so Loki can use binary-search instead of a full loop to fetch records). However, you're looking for a unique index, which is created with ensureUniqueIndex (check here, scroll down to Finding Documents, there's a section on unique indexes.). With that, you can use the collection method by(field, value) (which can even be curried if you only pass the field value), which uses the unique index to full potential (about 2x the speed of an indexed field). Remember that you need to explicitly call ensureUniqueIndex because unique indexes cannot be serialized and persisted.
update: once the ensureUniqueIndex method is called, the collection will throw an error if you try to insert a duplicate key record. If you have repository checked out you can take a look at spec/generic/unique.spec.js for an example ( here )
var loki = require('lokijs');
var db = new loki('test.json');
var users = db.addCollection('users', { indices: ['userID']});
users.ensureUniqueIndex('userID');
users.on('error',function(obj){
console.log('error ... adding 1 to userID');
obj.userID = obj.userID+1;
return obj;
});
users.insert(
{
'name': 'Anna',
'userID': 1
});
users.insert(
{
'name': 'Bernd',
'userID': 2
});
users.insert(
{
'name': 'Christa',
'userID': 3
});
db.save();
console.log(users.data);
try {
users.insert({'name': 'Dieter','userID': 2}); // this should error!!
} catch(e){
var i = 2+1;
users.insert({'name': 'Dieter','userID': i}); // this should error!!
}
db.save();
db2 = new loki('test.json');
db2.loadDatabase({}, function () {
var users2 = db2.getCollection('users')
console.log(users2.data);
});
Either users.on('error',...) nor try{ users.insert...} catch(e){// id+1} handles the thrown error
That's my console:
[ { name: 'Anna',
userID: 1,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 1 },
{ name: 'Bernd',
userID: 2,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 2 },
{ name: 'Christa',
userID: 3,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 3 } ]
Duplicate key for property userID: 2
[ { name: 'Anna',
userID: 1,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 1 },
{ name: 'Bernd',
userID: 2,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 2 },
{ name: 'Christa',
userID: 3,
meta: { revision: 0, created: 1436186694342, version: 0 },
'$loki': 3 },
{ name: 'Dieter',
userID: 2,
meta: { revision: 0, created: 0, version: 0 },
'$loki': 4 } ]

Sorting JSON and displaying in Tree using DOJO

I am learning DOJO 1.6.
I have data
var data = [
{ FirstName: 'xyz', Lastname: 'QSD', rollNo: '1', EntryDate: '2012-09-11T17:35:31.835+02:00' },
{ FirstName: 'abc', Lastname: 'qgr', rollNo: '2', EntryDate: '2012-08-11T17:35:31.835+02:00' }
{ FirstName: 'ert', Lastname: 'fgd', rollNo: '3', EntryDate: '2012-18-11T17:35:31.835+02:00' }
];
I want to sort it with respect to Last name or EntryDate and display in a tree format.
Thanks in Advance.
Multiple root data
data: [
{ id: 'world', name:'The earth', type:'planet', population: '6 billion'},
{ id: 'AF', name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km',
timezone: '-1 UTC to +4 UTC', parent: 'world'},
{ id: 'EG', name:'Egypt', type:'country', parent: 'AF' },
{ id: 'KE', name:'Kenya', type:'country', parent: 'AF' },
{ id: 'Nairobi', name:'Nairobi', type:'city', parent: 'KE' },
{ id: 'Mombasa', name:'Mombasa', type:'city', parent: 'KE' },
{ id: 'SD', name:'Sudan', type:'country', parent: 'AF' },
{ id: 'Khartoum', name:'Khartoum', type:'city', parent: 'SD' },
{ id: 'AS', name:'Asia', type:'continent', parent: 'world' },
{ id: 'CN', name:'China', type:'country', parent: 'AS' },
{ id: 'IN', name:'India', type:'country', parent: 'AS' },
{ id: 'RU', name:'Russia', type:'country', parent: 'AS' },
{ id: 'MN', name:'Mongolia', type:'country', parent: 'AS' },
{ id: 'OC', name:'Oceania', type:'continent', population:'21 million', parent: 'world'},
{ id: 'EU', name:'Europe', type:'continent', parent: 'world' },
{ id: 'DE', name:'Germany', type:'country', parent: 'EU' },
{ id: 'FR', name:'France', type:'country', parent: 'EU' },
{ id: 'ES', name:'Spain', type:'country', parent: 'EU' },
{ id: 'IT', name:'Italy', type:'country', parent: 'EU' },
{ id: 'NA', name:'North America', type:'continent', parent: 'world' },
{ id: 'SA', name:'South America', type:'continent', parent: 'world' }
],
Javascript Array has a native function, called sort. This will off-the-shelf sort the values alphabetically. For the purpose of sorting non-string-values, we need to supply a sortingfunction. Like so, in regards to Lastname:
data.sort(function(a,b) {
var _A=a.Lastname.toLowerCase(),
_B=b.Lastname.toLowerCase();
if (_A < _B) //sort string ascending
return -1
if (_A > _B)
return 1
return 0 //default return value (no sorting)
});
If youre sorting against a date, you would need to initialize _A and _B to a Date.
However, if youre aiming to represent the data in a dijit.Tree, there's inbuilt method for sorting the Store, lets wrap data into a dojo/data/ItemFileReadStore and show in a tree. Tree will have a model, using ItemFileWriteStore - so that items can be modified:
var sortableStore = new dojo.data.ItemFileReadStore({
data: {
identifier: 'rollNo',
items: data
},
comperatorMap: {
'EntryDate' : function(a,b) {
var _A = new Date(a), _B = new Date(b);
if(_A > _B) return 1;
else if(_A == _B) return 0;
else return -1;
}
});
Using 'store.fetch()' API while setting the 'sort' parameter, you control the order of returned items. The EntryDate you will have to create a comperatorMap of functions, as with Array.sort() in order to sort it properly. See the documentation.
var model = new dijit.tree.ForestStoreModel({
rootLabel: 'Names',
store: new dojo.data.ItemFileWriteStore({
data: {
identifier: 'rollNo',
items: data,
// blank, initially - can fill in with 'data' to show non-sorted items until sort is called
label: 'FirstName'
}
}) // blank itemsstore
});
var tree = new dijit.Tree({
model: model
});
OK, All set - but problem with .fetch is, it runs with callbacks (onComplete) and is difficult to control in a recursive manner. Instead, the functionality put in THIS FIDDLE duplicates the store data and then sorts it via native array sort - as opposed to using SimpleQueryEngine.
This will prove to give more reliable results - but does mess with DnD controllers and persist flag..
See how store can sort its items returned by fetch here: fiddle. This however only sorts one 'level' at a time and does not perform deep sorts.
IMO: The proper implementation of sort is a serverside sort, directly in the database query.
http://dojotoolkit.org/reference-guide/1.8/dojo/data/ItemFileReadStore.html#custom-sorting
http://dojotoolkit.org/reference-guide/1.8/dijit/Tree.html