/// <summary> /// Returns the user cache of the given user. If the user does not exist in CAM it is added. If /// the user is invalid according to cam no permissions are cached. /// </summary> /// <param name="user">The user to return cache for.</param> /// <returns>The user's cache.</returns> public async Task <UserCache> GetUserCacheAsync(IWebApiUser user) { Contract.Requires(user != null, "The user must not be null."); var userCache = cacheService.GetUserCache(user); logger.Info("User [{0}] is cached: {1}.", user, userCache != null); if (userCache == null) { logger.Info("Caching user [{0}] information.", user); var camUser = await userService.GetUserByIdAsync(user.Id); var isValidUser = false; IEnumerable <IPermission> permissions = new List <IPermission>(); if (camUser != null) { isValidUser = await userService.IsUserValidAsync(user.Id); if (isValidUser) { permissions = await GetUserPermissionsAsync(camUser.PrincipalId); } } else { camUser = new User(); } userCache = new UserCache(user, camUser, isValidUser, permissions); cacheService.Add(userCache); return(userCache); } else { return(userCache); } }