/// <summary> /// Removes attachment links to entity /// </summary> /// <param name="entity">entity (user)</param> /// <param name="attachment">attachment </param> /// <param name="permissions">Remove selected permissions. If null provided - removes all linked entities</param> public void RemoveAttachment(User entity, Server.Model.Attachment.Attachment attachment, ICollection <Permission> permissions = null) { //using (var tran = Db.Database.BeginTransaction()) //{ try { if (!Db.Set <User>().Any(it => it.Id == entity.Id)) { throw new ArgumentException("Can't Remove. User do not exist."); } if (!Db.Set <Server.Model.Attachment.Attachment>().Any(it => it.Id == attachment.Id)) { throw new ArgumentException("Can't Remove. Attachment do not exist."); } if (permissions == null) { var attachPerms = Db.Set <AttachmentPermission>() .Where(it => it.UserId == entity.Id && it.AttachmentId == attachment.Id); foreach (var attachmentPermission in attachPerms) { Db.Set <AttachmentPermission>().Remove(attachmentPermission); } } else { foreach (var permission in permissions) { var attachPerm = Db.Set <AttachmentPermission>() .Single( it => it.UserId == entity.Id && it.AttachmentId == attachment.Id && permission.Id == it.PermissionId); Db.Set <AttachmentPermission>().Remove(attachPerm); } } Db.SaveChanges(); // tran.Commit(); } catch (Exception ex) { LogEventManager.Logger.Error(ex.Message, ex); // tran.Rollback(); throw; } // } }
/// <summary> /// Links an attachment to entity /// </summary> /// <param name="entity">entity(user)</param> /// <param name="attachment">attachment</param> /// <param name="permissions">Permissions for attachment. If null provided - then we set full access and mark entity as owner</param> public void LinkAttachment(User entity, Server.Model.Attachment.Attachment attachment, ICollection <Permission> permissions = null) { //using (var tran = Db.Database.BeginTransaction()) //{ try { var isOwner = false; if (!Db.Set <User>().Any(it => it.Id == entity.Id)) { throw new ArgumentException("Cant link. User do not exist"); } if (!Db.Set <Server.Model.Attachment.Attachment>().Any(it => it.Id == attachment.Id)) { throw new ArgumentException("Cant link. Attachment do not exist"); } if (permissions == null) { permissions = Db.Set <Permission>().Where(it => it.IsActive).ToList(); isOwner = true; } foreach (var permission in permissions) { Db.Set <AttachmentPermission>().Add(new AttachmentPermission { PermissionId = permission.Id, AttachmentId = attachment.Id, UserId = entity.Id, IsOwner = isOwner }); } Db.SaveChanges(); // tran.Commit(); } catch (Exception ex) { LogEventManager.Logger.Error(ex.Message, ex); // tran.Rollback(); throw; } // } }