public bool SetUserPermissionsAndCode(string uniqueName, string code) { try { var userPermissions = (from u in _context.Users join up in _context.UsersPermissions on u.UniqueName equals up.UniqueName join r in _context.Resources on up.ResourceID equals r.ResourceID join m in _context.Modules on r.ModuleID equals m.ModuleID where up.UniqueName == uniqueName && r.ResourceSequence > 0 orderby r.CategorySequence, r.ResourceSequence select new PermissionsToBeCachedViewModel { Endpoint = m.Endpoint, Module = m.ModuleName, Resource = r.ResourceName, Category = r.Category, DisplayName = r.DisplayName }).ToList(); var propertiesToCache = new CacheViewModel(code, userPermissions); _cache.Set(uniqueName, Util.GetBytes(propertiesToCache.ToString())); return(true); } catch (Exception) { throw; } }
//Updates permissions on database and cache public bool UpdateUserPermissions(string uniqueName, PermissionsViewModel newUserPermissions) { try { //Delete old permissions from the database: var oldPermissions = _context.UsersPermissions.Where(up => up.UniqueName == uniqueName); _context.UsersPermissions.RemoveRange(oldPermissions); //_context.SaveChanges(); //Update the database //Permissions: foreach (var permissionToQuery in newUserPermissions.permissions) { var resourceReturned = _context.Resources.First(r => r.Category == permissionToQuery.category && r.DisplayName == permissionToQuery.resource); _context.UsersPermissions.Add(new UsersPermission() { UniqueName = uniqueName, ResourceID = resourceReturned.ResourceID }); } //User: var user = _context.Users.First(u => u.UniqueName == uniqueName); if (newUserPermissions.permissions.Count > 0) { user.Status = PermissionStatus.Permissions_Granted; } else { user.Status = PermissionStatus.Permissions_Denied; } _context.SaveChanges(); //Update the cache CacheViewModel propertiesToCache; try { //User is on cache: var propertiesSerializedCached = Util.GetString(_cache.Get(uniqueName)); propertiesToCache = new CacheViewModel(propertiesSerializedCached); } catch { //User is not on cache: propertiesToCache = new CacheViewModel(); } var userPermissions = (from up in _context.UsersPermissions join r in _context.Resources on up.ResourceID equals r.ResourceID join m in _context.Modules on r.ModuleID equals m.ModuleID where up.UniqueName == uniqueName && r.ResourceSequence > 0 orderby r.CategorySequence, r.ResourceSequence select new PermissionsToBeCachedViewModel { Endpoint = m.Endpoint, Module = m.ModuleName, Resource = r.ResourceName, Category = r.Category, DisplayName = r.DisplayName }).ToList(); if (userPermissions != null) { propertiesToCache.Permissions = userPermissions; _cache.Set(uniqueName, Util.GetBytes(propertiesToCache.ToString())); return(true); } else { return(false); } } catch (Exception) { throw; } }