Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let tokenItem = localStorage.getItem("token");
if (tokenItem === null) {
this.currentUser = this.createAnonymousUser();
this.currentUserChanged$.emit(this.currentUser);
return Observable.of(null);
} else {
let token = tokenItem ? JSON.parse(tokenItem.toString()) : null;
var username = token.userName;
var query = EntityQuery
.from("Users")
.expand("ResourcePoolSet")
.where("UserName", "eq", username)
.using(FetchStrategy.FromServer);
return this.executeQuery(query)
.map((data: any): void => {
// If the response has an entity, use that, otherwise create an anonymous user
if (data.results.length > 0) {
this.currentUser = data.results[0];
this.fetchedUsers.push(this.currentUser.UserName);
} else {
localStorage.removeItem("token"); // TODO Invalid token, expired?
let tokenItem = localStorage.getItem("token");
if (tokenItem === null) {
this.currentUser = this.createAnonymousUser();
this.currentUserChanged$.emit(this.currentUser);
return Observable.of(null);
} else {
let token = tokenItem ? JSON.parse(tokenItem.toString()) : null;
var username = token.userName;
var query = EntityQuery
.from("Users")
.expand("ResourcePoolSet")
.where("UserName", "eq", username)
.using(FetchStrategy.FromServer);
return this.executeQuery(query)
.map((data: any): void => {
// If the response has an entity, use that, otherwise create an anonymous user
if (data.results.length > 0) {
this.currentUser = data.results[0];
this.fetchedUsers.push(this.currentUser.UserName);
} else {
localStorage.removeItem("token"); // TODO Invalid token, expired?
getResourcePoolExpanded(resourcePoolUniqueKey: any) {
// TODO Validations?
var fetchedEarlier = false;
// If it's not newly created, check the fetched list
fetchedEarlier = this.fetchedList.some((item: any) => (resourcePoolUniqueKey.username === item.username
&& resourcePoolUniqueKey.resourcePoolKey === item.resourcePoolKey));
// Prepare the query
var query = EntityQuery.from("ResourcePool");
// Is authorized? No, then get only the public data, yes, then get include user's own records
if (this.dataService.currentUser.isAuthenticated()) {
query = query.expand("User, UserResourcePoolSet, ElementSet.ElementFieldSet.UserElementFieldSet, ElementSet.ElementItemSet.ElementCellSet.UserElementCellSet");
} else {
query = query.expand("User, ElementSet.ElementFieldSet, ElementSet.ElementItemSet.ElementCellSet");
}
var userNamePredicate = new Predicate("User.UserName", "eq", resourcePoolUniqueKey.username);
var resourcePoolKeyPredicate = new Predicate("Key", "eq", resourcePoolUniqueKey.resourcePoolKey);
query = query.where(userNamePredicate.and(resourcePoolKeyPredicate));
// From server or local?
if (!fetchedEarlier) {
query = query.using(FetchStrategy.FromServer);
getUser(username: string): Observable {
const query = EntityQuery
.from("Users")
.expand("ProjectSet")
.where("UserName", "eq", username);
return this.appEntityManager.executeQueryObservable(query).pipe(
map((response) => {
// If there is no result
if (response.results.length === 0) {
return null;
}
return response.results[0];
}));
}
getProjectExpanded(projectId: number, forceRefresh = false) {
// Prepare the query
let query = EntityQuery.from("Project").where("Id", "eq", projectId);
// Is authorized? No, then get only the public data, yes, then get include user's own records
query = this.authService.currentUser.isAuthenticated()
? query.expand("User, ElementSet.ElementFieldSet.UserElementFieldSet, ElementSet.ElementItemSet.ElementCellSet.UserElementCellSet")
: query.expand("User, ElementSet.ElementFieldSet, ElementSet.ElementItemSet.ElementCellSet");
return this.appEntityManager.executeQueryObservable(query, forceRefresh).pipe(
map(response => {
return response.results[0] || null;
}));
}
getResourcePoolSet(searchKey: any) {
searchKey = typeof searchKey !== "undefined" ? searchKey : "";
var query = EntityQuery
.from("ResourcePool")
.expand(["User"])
.orderBy("Name");
if (searchKey !== "") {
var resourcePoolNamePredicate = new Predicate("Name", "contains", searchKey);
var userNamePredicate = new Predicate("User.UserName", "contains", searchKey);
query = query.where(resourcePoolNamePredicate.or(userNamePredicate));
}
// Prepare the query
//if (fetchFromServer) { // From remote
query = query.using(FetchStrategy.FromServer);
// fetchFromServer = false; // Do it only once per user
//}
//else { // From local
private ensureRolesEntities(): Observable {
if (this.appEntityManager.getEntities("Role").length > 0) {
return observableOf(null);
} else {
const query = EntityQuery.from("Roles");
return this.appEntityManager.executeQueryObservable(query).pipe(map(() => { }));
}
}