Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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?
this.currentUser = this.createAnonymousUser();
}
getUser(username: string) {
// Already fetched, then query locally
let alreadyFetched = this.fetchedUsers.indexOf(username) > -1;
let query = EntityQuery
.from("Users")
.expand("ResourcePoolSet")
.where("UserName", "eq", username);
// From server or local?
if (alreadyFetched) {
query = query.using(FetchStrategy.FromLocalCache);
} else {
query = query.using(FetchStrategy.FromServer);
}
return this.executeQuery(query)
.map((response: any) => {
// If there is no result
if (response.results.length === 0) {
return null;
}
var user = response.results[0];
// Add to fetched list
if (!alreadyFetched) {
this.fetchedUsers.push(user.UserName);
}
// 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);
} else {
query = query.using(FetchStrategy.FromLocalCache);
}
return this.dataService.executeQuery(query)
.map((response: any): any => {
// If there is no cmrp with this Id, return null
if (response.results.length === 0) {
return null;
}
// ResourcePool
var resourcePool = response.results[0];
// Todo Is there a better way of doing this? / coni2k - 25 Feb. '17