public GXDeviceDeleteResponse Post(GXDeviceDeleteRequest request) { IAuthSession s = this.GetSession(false); int id = Convert.ToInt32(s.Id); if (id == 0) { throw new ArgumentException("Remove failed. Invalid session ID."); } //Normal user can't remove device. if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s)) { throw new ArgumentException("Access denied."); } bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s); List <GXEventsItem> events = new List <GXEventsItem>(); bool permanently = request.Permanently; List <GXAmiDevice> devices = new List <GXAmiDevice>(); lock (Db) { foreach (ulong it in request.DeviceIDs) { if (it == 0) { throw new ArgumentException("ID is required"); } if (!superAdmin && !CanUserAccessDevice(Db, id, it)) { throw new ArgumentException("Access denied."); } #if !SS4 GXAmiDevice device = Db.QueryById <GXAmiDevice>(it); #else GXAmiDevice device = Db.SingleById <GXAmiDevice>(it); #endif devices.Add(device); events.Add(new GXEventsItem(ActionTargets.Device, Actions.Remove, device)); } } //Notify before delete or DC is not notified because device is not found from the DB. AppHost host = this.ResolveService <AppHost>(); host.SetEvents(Db, this.Request, id, events); lock (Db) { foreach (GXAmiDevice it in devices) { if (permanently) { Db.DeleteById <GXAmiDevice>(it.Id); } else { it.Removed = DateTime.Now.ToUniversalTime(); Db.UpdateOnly(it, p => p.Removed, p => p.Id == it.Id); } } } return(new GXDeviceDeleteResponse()); }
public GXDeviceDeleteResponse Post(GXDeviceDeleteRequest request) { IAuthSession s = this.GetSession(false); int id = Convert.ToInt32(s.Id); if (id == 0) { throw new ArgumentException("Remove failed. Invalid session ID."); } //Normal user can't remove device. if (!GuruxAMI.Server.GXBasicAuthProvider.CanUserEdit(s)) { throw new ArgumentException("Access denied."); } bool superAdmin = GuruxAMI.Server.GXBasicAuthProvider.IsSuperAdmin(s); List<GXEventsItem> events = new List<GXEventsItem>(); bool permanently = request.Permanently; List<GXAmiDevice> devices = new List<GXAmiDevice>(); lock (Db) { foreach (ulong it in request.DeviceIDs) { if (it == 0) { throw new ArgumentException("ID is required"); } if (!superAdmin && !CanUserAccessDevice(Db, id, it)) { throw new ArgumentException("Access denied."); } #if !SS4 GXAmiDevice device = Db.QueryById<GXAmiDevice>(it); #else GXAmiDevice device = Db.SingleById<GXAmiDevice>(it); #endif devices.Add(device); events.Add(new GXEventsItem(ActionTargets.Device, Actions.Remove, device)); } } //Notify before delete or DC is not notified because device is not found from the DB. AppHost host = this.ResolveService<AppHost>(); host.SetEvents(Db, this.Request, id, events); lock (Db) { foreach (GXAmiDevice it in devices) { if (permanently) { Db.DeleteById<GXAmiDevice>(it.Id); } else { it.Removed = DateTime.Now.ToUniversalTime(); Db.UpdateOnly(it, p => p.Removed, p => p.Id == it.Id); } } } return new GXDeviceDeleteResponse(); }