/// <summary> /// Method to select an AclGroup by id. /// </summary> /// <returns>An AclGroup entity or null if not found.</returns> private AclGroupEntity SingleIdDefaultOrNull(IQueryable <AclGroupEntity> query, AclGroupOptionsSelect op, bool nullable = false) { if (op.PrimaryKey <= 0) { if (nullable) { return(null); } else { return(new AclGroupEntity()); } } AclGroupEntity item = query.SingleOrDefault(x => x.AclGroupId == op.PrimaryKey); // Check if user is found, return null instead of default. if (item == null || item.AclGroupId == 0) { if (nullable) { return(null); } else { return(new AclGroupEntity()); } } return(item); }
/// <summary> /// Method to update an Entity. /// </summary> /// <param name="entity">An Entity to update.</param> /// <returns>The updated Entity.</returns> public async Task <AclGroupEntity> UpdateAsync(AclGroupEntity entity) { // Remove Users In AclGroups dependencies. DeleteDependencyUsers(entity); // Remove Sections In AclGroups dependencies. if (entity.UsersInAclGroups.DepPKeysRemoved.Count > 0) { await DeleteDependencyAsync( new EntityManagerDeleteDependency { Name = "UsersInAclGroups", key = "AclGroupId", keyList = "UserId" }, entity.PrimaryKey, entity.UsersInAclGroups.DepPKeysRemoved ); entity.UsersInAclGroups.DepPKeysRemoved.Clear(); } // Update item informations. entity = Connector.Update(entity).Entity; await SaveAsync(); // Return the updated item. return(entity); }
/// <summary> /// /// </summary> /// <param name="id">The primary key or id of the <see cref="SectionEntity"/></param> /// <param name="user">The <see cref="UserEntity"/>.</param> /// <param name="entity"></param> /// <returns></returns> private SectionEntity GetUserSection(int id, UserEntity user) { SectionEntity entity = null; // Try to found section in dependencies. foreach (UsersInAclGroups group in user.UsersInAclGroups) { IQueryable <AclGroupEntity> groups = Connector.AclGroups; groups = groups.Include(x => x.SectionsInAclGroups); AclGroupEntity ag = groups.SingleOrDefault(x => x.PrimaryKey == group.AclGroupId); if (ag.PrimaryKey == 0 || ag.SectionsInAclGroups.Count == 0) { continue; } var dep = ag.SectionsInAclGroups.ToList().Find(x => x.SectionId == id); if (dep != null) { IQueryable <SectionEntity> sections = Connector.Sections; sections = sections.Include(x => x.SectionsInAclGroups); sections = sections.Include(x => x.AlbumsInSections); entity = sections.SingleOrDefault(x => x.PrimaryKey == id); } if (entity?.PrimaryKey > 0) { break; } } return(entity); }
/// <summary> /// Method to remove association between a AclAction and an AclGroup. /// </summary> /// <param name="entity"></param> /// <returns>Async task with modified AclGroup entity as result.</returns> public async void CleanAclActionDependencyAsync(AclGroupEntity entity) { string action = string.Join(",", entity.AclActionsPKeys); int result = await Connector.Database.ExecuteSqlCommandAsync( $"DELETE FROM AclGroupsInAclActions WHERE AclGroupId = {entity.PrimaryKey} AND AclActionId NOT IN ({action})" ); Save(); }
/// <summary> /// Method called on AclGroup default change event. /// </summary> /// <param name="sender">The object sender of the event.</param> /// <param name="e">Event arguments.</param> private void UcDataGridAclGroups_DefaultChanged(object sender, EntityChangesEventArgs e) { log.Info("Setting default Section. Please wait..."); AclGroupEntity newEntity = (AclGroupEntity)e.NewEntity; AclGroupEntityCollection.SetDefault(newEntity); Model.LoadAclGroups(); log.Info("Setting default Section. Done."); }
/// <summary> /// Method to delete an <see cref="AclGroupEntity"/>. /// </summary> /// <param name="aclGroupId">An <see cref="AclGroupEntity"/> primary key.</param> /// <returns>The deleted <see cref="AclGroupEntity"/>.</returns> public AclGroupEntity Delete(int alGroupId) { AclGroupEntity item = SingleOrNull(new AclGroupOptionsSelect { PrimaryKey = alGroupId }); using (Db.Context) { item = AclGroupManager.Delete(item); } return(item); }
/// <summary> /// Method to add new <see cref="AclGroupEntity"/>. /// </summary> /// <param name="AclGroup">The <see cref="AclGroupEntity"/> to add.</param> /// <returns>The added <see cref="AclGroupEntity"/>.</returns> public AclGroupEntity Add(AclGroupEntity entity) { using (Db.Context) { entity = AclGroupManager.Add(entity); if (entity.IsDefault) { SetDefault(entity.PrimaryKey); } return(entity); } }
/// <summary> /// Method to delete sections in aclgroups associations. /// </summary> /// <param name="entity">The entity to process with.</param> private async void DeleteDependencySections(AclGroupEntity entity) { // Remove Sections In AclGroups dependencies. if (entity.SectionsInAclGroups.DepPKeysRemoved.Count > 0) { await DeleteDependencyAsync( new EntityManagerDeleteDependency { Name = "SectionsInAclGroups", key = "AclGroupId", keyList = "SectionId" }, entity.PrimaryKey, entity.SectionsInAclGroups.DepPKeysRemoved ); entity.SectionsInAclGroups.DepPKeysRemoved.Clear(); } }
/// <summary> /// Method to delete <see cref="UsersInAclGroups"/> associations. /// </summary> /// <param name="entity">The entity to process with.</param> private async void DeleteDependencyUsers(AclGroupEntity entity) { if (entity.UsersInAclGroups.DepPKeysRemoved.Count > 0) { await DeleteDependencyAsync( new EntityManagerDeleteDependency { Name = "UsersInAclGroups", key = "AclGroupId", keyList = "UserId" }, entity.PrimaryKey, entity.UsersInAclGroups.DepPKeysRemoved ); entity.UsersInAclGroups.DepPKeysRemoved.Clear(); log.Debug("Delete users in aclgroups associations. Done."); } }
/// <summary> /// Method called on AclGroup view changes event. /// </summary> /// <param name="sender">The object sender of the event.</param> /// <param name="e">Event arguments.</param> private void UcDataGridAclGroups_AclGroupAdded(object sender, EntityChangesEventArgs e) { log.Info("Saving new AclGroup informations. Please wait..."); AclGroupEntity item = (AclGroupEntity)e.NewEntity; Model.AclGroups.Items.Add(item); AclGroupEntityCollection.DbInsert(new List <AclGroupEntity> { item }); Model.LoadAll(); log.Info("Saving new AclGroup informations. Done."); }
/// <summary> /// Method called on AclGroup view delete event. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e"></param> private void UcDataGridAclGroups_AclGroupDeleted(object sender, EntityChangesEventArgs e) { log.Info("Deleting AclGroup(s). Please wait..."); AclGroupEntity item = (AclGroupEntity)e.NewEntity; // Remove item from list. Model.AclGroups.Items.Remove(item); // Delete item from database. AclGroupEntityCollection.DbDelete(new List <AclGroupEntity> { item }); Model.LoadAll(); log.Info("Deleting AclGroup(s). Done."); }
/// <summary> /// Method called on AclGroup view add event. /// </summary> /// <param name="sender">The object sender of the event.</param> /// <param name="e">Event arguments.</param> private void UcDataGridAclGroups_AclGroupChanged(object sender, EntityChangesEventArgs e) { log.Info("Saving AclGroup informations. Please wait..."); AclGroupEntity newEntity = (AclGroupEntity)e.NewEntity; AclGroupEntity old = Model.AclGroups.Items.Single(x => x.AclGroupId == newEntity.AclGroupId); int index = Model.AclGroups.Items.IndexOf(old); Model.AclGroups.Items[index] = newEntity; AclGroupEntityCollection.DbUpdateAsync(new List <AclGroupEntity> { newEntity }, new List <AclGroupEntity> { old }); Model.LoadAll(); log.Info("Saving AclGroup informations. Done."); }
/// <summary> /// /// </summary> /// <param name="id">The primary key or id of the <see cref="SectionEntity"/></param> /// <param name="user">The <see cref="UserEntity"/>.</param> /// <param name="entity"></param> /// <returns></returns> public static SectionEntity GetUserSection(int id, UserEntity user) { SectionEntity entity = null; // Try to found section in dependencies. foreach (int groupPK in user.AclGroupsPKeys) { AclGroupEntity ag = Database.AclGroups.SingleOrDefault ( new AclGroupOptionsSelect { PrimaryKey = groupPK, Dependencies = new List <EnumEntitiesDependencies> { EnumEntitiesDependencies.All } } ); var dep = ag.SectionsInAclGroups.ToList().Find(x => x.SectionId == id); if (dep != null) { entity = Database.Sections.SingleOrDefault ( new SectionOptionsSelect { PrimaryKey = id, Dependencies = new List <EnumEntitiesDependencies> { EnumEntitiesDependencies.All } } ); } if (entity != null) { break; } } return(entity); }
/// <summary> /// Method to update an <see cref="AclGroupEntity"/>. /// </summary> /// <param name="entity">The AclGroup Entity</param> /// <returns>The updated <see cref="AclGroupEntity"/>.</returns> public async Task <AclGroupEntity> UpdateAsync(AclGroupEntity entity) { using (Db.Context) { // Try to attach entity to the database context. try { Db.Context.Attach(entity); } catch { throw new System.Exception("Error on database Context Attach AclGroup"); } // Update entity. entity = await AclGroupManager.UpdateAsync(entity); if (entity.IsDefault) { SetDefault(entity.PrimaryKey); } // Hack to delete unassociated dependencies. //await CleanDependencies_Async("AclGroupsInAclActions", "AclActionId", entity.PrimaryKey, entity.AclActionsPKeys); //await CleanDependencies_Async("SectionsInAclGroups", "SectionId", entity.PrimaryKey, entity.SectionsPKs); //await CleanDependencies_Async("UsersInAclGroups", "UserId", entity.PrimaryKey, entity.UsersPKeys); return(entity); } }
/// <summary> /// Method to add new <see cref="AclGroupEntity"/> asynchronously. /// </summary> /// <param name="AclGroup">The <see cref="AclGroupEntity"/> to add.</param> /// <returns>The added <see cref="AclGroupEntity"/>.</returns> public Task <AclGroupEntity> AddAsync(AclGroupEntity entity) => Task.Run(() => { return(Add(entity)); });