public StoredSecurityEntity LoadStoredSecurityEntity(int entityId) { StoredSecurityEntity entity = null; _storage.Entities.TryGetValue(entityId, out entity); return(entity); }
/// <summary> /// Writes the given entity to the database. If it exists before writing, the operation will be skipped. /// </summary> public void InsertSecurityEntity(StoredSecurityEntity entity) { using (var db = Db()) { var origEntity = LoadEFEntity(entity.Id, db); if (origEntity != null) { return; } db.EFEntities.Add(new EFEntity { Id = entity.Id, OwnerId = entity.nullableOwnerId, ParentId = entity.nullableParentId, IsInherited = entity.IsInherited }); try { db.SaveChanges(); } catch (DbUpdateException) { // entity already exists, that's ok } } }
public void UpdateSecurityEntity(StoredSecurityEntity entity) { var oldEntity = LoadStoredSecurityEntity(entity.Id); if (oldEntity == null) { throw new EntityNotFoundException("Cannot update entity because it does not exist: " + entity.Id); } _storage.Entities[entity.Id] = entity; }
private void FindAces(StoredSecurityEntity entity) { lock (_acesLock) { foreach (var child in _storage.Entities.Values.Where(e => e.ParentId == entity.Id).ToArray()) { FindAces(child); } _aces.AddRange(_storage.Aces.Where(a => a.EntityId == entity.Id)); } }
public void InsertSecurityEntity(StoredSecurityEntity entity) { var origEntity = LoadStoredSecurityEntity(entity.Id); if (origEntity != null) { return; } _storage.Entities[entity.Id] = entity; }
private static void CreateTestEntities_Big(int currentLevel, StoredSecurityEntity root, Dictionary <int, StoredSecurityEntity> storage) { if (currentLevel >= _maxLevel) { return; } for (int i = 0; i < _levelWidth; i++) { var entity = CreateEntity_Big(root, root.OwnerId, storage); CreateTestEntities_Big(currentLevel + 1, entity, storage); } }
/// <summary> /// Updates the given entity to the database. If it does not exist before updating, /// a SecurityStructureException must be thrown. /// </summary> public void UpdateSecurityEntity(StoredSecurityEntity entity) { var exceptions = new List <Exception>(); for (var retry = 3; retry > 0; retry--) { try { using (var db = Db()) { var oldEntity = LoadEFEntity(entity.Id, db); if (oldEntity == null) { throw new EntityNotFoundException("Cannot update entity because it does not exist: " + entity.Id); } oldEntity.OwnerId = entity.nullableOwnerId; oldEntity.ParentId = entity.nullableParentId; oldEntity.IsInherited = entity.IsInherited; db.SaveChanges(); return; } } catch (DbUpdateConcurrencyException ex) { // handling concurrency exceptions.Add(ex); // if this is not the last iteration, wait a bit before retrying if (retry > 0) { Thread.Sleep(10); } } catch (Exception ex) { exceptions.Add(ex); // unknown exception: skip out of the loop immediately break; } } // the loop was finished after several attenpts if (exceptions.Count > 0) { throw new SecurityStructureException( "Cannot update entity because of concurrency: " + entity.Id, new AggregateException(exceptions)); } }
private static StoredSecurityEntity CreateEntity_Big(StoredSecurityEntity parentEntity, int ownerId, Dictionary <int, StoredSecurityEntity> storage) { var entity = new StoredSecurityEntity { Id = _id++, ParentId = parentEntity == null ? default(int) : parentEntity.Id, IsInherited = true, OwnerId = ownerId, }; storage[entity.Id] = entity; return(entity); }
private static void CreateEntity(string name, string parentName, TestUser owner, Dictionary <int, StoredSecurityEntity> storage) { var entityId = Id(name); var parentEntityId = parentName == null ? default : Id(parentName); storage.TryGetValue(parentEntityId, out _); var entity = new StoredSecurityEntity { Id = entityId, ParentId = parentEntityId, IsInherited = true, OwnerId = owner.Id }; storage[entityId] = entity; }