Use Prettier JS (#5017)

* Adds prettier

* Run lint before tests
This commit is contained in:
Florent Vilmart
2018-09-01 13:58:06 -04:00
committed by GitHub
parent 189cd259ee
commit d83a0b6808
240 changed files with 41098 additions and 29020 deletions

View File

@@ -5,7 +5,14 @@ const Parse = require('parse/node');
// An Auth object tells you who is requesting something and whether
// the master key was used.
// userObject is a Parse.User and can be null if there's no user.
function Auth({ config, cacheController = undefined, isMaster = false, isReadOnly = false, user, installationId }) {
function Auth({
config,
cacheController = undefined,
isMaster = false,
isReadOnly = false,
user,
installationId,
}) {
this.config = config;
this.cacheController = cacheController || (config && config.cacheController);
this.installationId = installationId;
@@ -47,15 +54,27 @@ function nobody(config) {
return new Auth({ config, isMaster: false });
}
// Returns a promise that resolves to an Auth object
const getAuthForSessionToken = async function({ config, cacheController, sessionToken, installationId }) {
const getAuthForSessionToken = async function({
config,
cacheController,
sessionToken,
installationId,
}) {
cacheController = cacheController || (config && config.cacheController);
if (cacheController) {
const userJSON = await cacheController.user.get(sessionToken);
if (userJSON) {
const cachedUser = Parse.Object.fromJSON(userJSON);
return Promise.resolve(new Auth({config, cacheController, isMaster: false, installationId, user: cachedUser}));
return Promise.resolve(
new Auth({
config,
cacheController,
isMaster: false,
installationId,
user: cachedUser,
})
);
}
}
@@ -63,27 +82,40 @@ const getAuthForSessionToken = async function({ config, cacheController, session
if (config) {
const restOptions = {
limit: 1,
include: 'user'
include: 'user',
};
const query = new RestQuery(config, master(config), '_Session', { sessionToken }, restOptions);
const query = new RestQuery(
config,
master(config),
'_Session',
{ sessionToken },
restOptions
);
results = (await query.execute()).results;
} else {
results = (await new Parse.Query(Parse.Session)
.limit(1)
.include('user')
.equalTo('sessionToken', sessionToken)
.find({ useMasterKey: true })).map((obj) => obj.toJSON())
.find({ useMasterKey: true })).map(obj => obj.toJSON());
}
if (results.length !== 1 || !results[0]['user']) {
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'Invalid session token');
throw new Parse.Error(
Parse.Error.INVALID_SESSION_TOKEN,
'Invalid session token'
);
}
const now = new Date(),
expiresAt = results[0].expiresAt ? new Date(results[0].expiresAt.iso) : undefined;
expiresAt = results[0].expiresAt
? new Date(results[0].expiresAt.iso)
: undefined;
if (expiresAt < now) {
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN,
'Session token is expired.');
throw new Parse.Error(
Parse.Error.INVALID_SESSION_TOKEN,
'Session token is expired.'
);
}
const obj = results[0]['user'];
delete obj.password;
@@ -93,25 +125,49 @@ const getAuthForSessionToken = async function({ config, cacheController, session
cacheController.user.put(sessionToken, obj);
}
const userObject = Parse.Object.fromJSON(obj);
return new Auth({ config, cacheController, isMaster: false, installationId, user: userObject });
return new Auth({
config,
cacheController,
isMaster: false,
installationId,
user: userObject,
});
};
var getAuthForLegacySessionToken = function({ config, sessionToken, installationId }) {
var getAuthForLegacySessionToken = function({
config,
sessionToken,
installationId,
}) {
var restOptions = {
limit: 1
limit: 1,
};
var query = new RestQuery(config, master(config), '_User', { sessionToken }, restOptions);
return query.execute().then((response) => {
var query = new RestQuery(
config,
master(config),
'_User',
{ sessionToken },
restOptions
);
return query.execute().then(response => {
var results = response.results;
if (results.length !== 1) {
throw new Parse.Error(Parse.Error.INVALID_SESSION_TOKEN, 'invalid legacy session token');
throw new Parse.Error(
Parse.Error.INVALID_SESSION_TOKEN,
'invalid legacy session token'
);
}
const obj = results[0];
obj.className = '_User';
const userObject = Parse.Object.fromJSON(obj);
return new Auth({ config, isMaster: false, installationId, user: userObject });
return new Auth({
config,
isMaster: false,
installationId,
user: userObject,
});
});
}
};
// Returns a promise that resolves to an array of role names
Auth.prototype.getUserRoles = function() {
@@ -131,21 +187,27 @@ Auth.prototype.getUserRoles = function() {
Auth.prototype.getRolesForUser = function() {
if (this.config) {
const restWhere = {
'users': {
users: {
__type: 'Pointer',
className: '_User',
objectId: this.user.id
}
objectId: this.user.id,
},
};
const query = new RestQuery(this.config, master(this.config), '_Role', restWhere, {});
const query = new RestQuery(
this.config,
master(this.config),
'_Role',
restWhere,
{}
);
return query.execute().then(({ results }) => results);
}
return new Parse.Query(Parse.Role)
.equalTo('users', this.user)
.find({ useMasterKey: true })
.then((results) => results.map((obj) => obj.toJSON()));
}
.then(results => results.map(obj => obj.toJSON()));
};
// Iterates through the role tree and compiles a user's roles
Auth.prototype._loadRoles = async function() {
@@ -169,15 +231,21 @@ Auth.prototype._loadRoles = async function() {
return this.userRoles;
}
const rolesMap = results.reduce((m, r) => {
m.names.push(r.name);
m.ids.push(r.objectId);
return m;
}, {ids: [], names: []});
const rolesMap = results.reduce(
(m, r) => {
m.names.push(r.name);
m.ids.push(r.objectId);
return m;
},
{ ids: [], names: [] }
);
// run the recursive finding
const roleNames = await this._getAllRolesNamesForRoleIds(rolesMap.ids, rolesMap.names);
this.userRoles = roleNames.map((r) => {
const roleNames = await this._getAllRolesNamesForRoleIds(
rolesMap.ids,
rolesMap.names
);
this.userRoles = roleNames.map(r => {
return 'role:' + r;
});
this.fetchedRoles = true;
@@ -192,38 +260,45 @@ Auth.prototype.cacheRoles = function() {
}
this.cacheController.role.put(this.user.id, Array(...this.userRoles));
return true;
}
};
Auth.prototype.getRolesByIds = function(ins) {
const roles = ins.map((id) => {
const roles = ins.map(id => {
return {
__type: 'Pointer',
className: '_Role',
objectId: id
}
objectId: id,
};
});
const restWhere = { 'roles': { '$in': roles }};
const restWhere = { roles: { $in: roles } };
// Build an OR query across all parentRoles
if (!this.config) {
return new Parse.Query(Parse.Role)
.containedIn('roles', ins.map((id) => {
const role = new Parse.Object(Parse.Role);
role.id = id;
return role;
}))
.containedIn(
'roles',
ins.map(id => {
const role = new Parse.Object(Parse.Role);
role.id = id;
return role;
})
)
.find({ useMasterKey: true })
.then((results) => results.map((obj) => obj.toJSON()));
.then(results => results.map(obj => obj.toJSON()));
}
return new RestQuery(this.config, master(this.config), '_Role', restWhere, {})
.execute()
.then(({ results }) => results);
}
};
// Given a list of roleIds, find all the parent roles, returns a promise with all names
Auth.prototype._getAllRolesNamesForRoleIds = function(roleIDs, names = [], queriedRoles = {}) {
const ins = roleIDs.filter((roleID) => {
Auth.prototype._getAllRolesNamesForRoleIds = function(
roleIDs,
names = [],
queriedRoles = {}
) {
const ins = roleIDs.filter(roleID => {
const wasQueried = queriedRoles[roleID] !== true;
queriedRoles[roleID] = true;
return wasQueried;
@@ -234,32 +309,39 @@ Auth.prototype._getAllRolesNamesForRoleIds = function(roleIDs, names = [], queri
return Promise.resolve([...new Set(names)]);
}
return this.getRolesByIds(ins).then((results) => {
// Nothing found
if (!results.length) {
return Promise.resolve(names);
}
// Map the results with all Ids and names
const resultMap = results.reduce((memo, role) => {
memo.names.push(role.name);
memo.ids.push(role.objectId);
return memo;
}, {ids: [], names: []});
// store the new found names
names = names.concat(resultMap.names);
// find the next ones, circular roles will be cut
return this._getAllRolesNamesForRoleIds(resultMap.ids, names, queriedRoles)
}).then((names) => {
return Promise.resolve([...new Set(names)])
})
}
return this.getRolesByIds(ins)
.then(results => {
// Nothing found
if (!results.length) {
return Promise.resolve(names);
}
// Map the results with all Ids and names
const resultMap = results.reduce(
(memo, role) => {
memo.names.push(role.name);
memo.ids.push(role.objectId);
return memo;
},
{ ids: [], names: [] }
);
// store the new found names
names = names.concat(resultMap.names);
// find the next ones, circular roles will be cut
return this._getAllRolesNamesForRoleIds(
resultMap.ids,
names,
queriedRoles
);
})
.then(names => {
return Promise.resolve([...new Set(names)]);
});
};
const createSession = function(config, {
userId,
createdWith,
installationId,
additionalSessionData,
}) {
const createSession = function(
config,
{ userId, createdWith, installationId, additionalSessionData }
) {
const token = 'r:' + cryptoUtils.newToken();
const expiresAt = config.generateSessionExpiresAt();
const sessionData = {
@@ -267,15 +349,15 @@ const createSession = function(config, {
user: {
__type: 'Pointer',
className: '_User',
objectId: userId
objectId: userId,
},
createdWith,
restricted: false,
expiresAt: Parse._encode(expiresAt)
expiresAt: Parse._encode(expiresAt),
};
if (installationId) {
sessionData.installationId = installationId
sessionData.installationId = installationId;
}
Object.assign(sessionData, additionalSessionData);
@@ -284,9 +366,16 @@ const createSession = function(config, {
return {
sessionData,
createSession: () => new RestWrite(config, master(config), '_Session', null, sessionData).execute()
}
}
createSession: () =>
new RestWrite(
config,
master(config),
'_Session',
null,
sessionData
).execute(),
};
};
module.exports = {
Auth,