/// <summary> /// Удаление администратора /// </summary> /// <param name="SessionKey"></param> /// <param name="AdminId">идентификатор администратора</param> public void DeleteAdmin(String SessionKey, int AdminId) { CheckSession(SessionKey); using (var db = new DataBase()) { IEnumerable <AccessToPanel> DButton = (from c in db.AccesToes where c.ID_Admin == AdminId select new AccessToPanel { ID_Button = c.ID_Button }).ToList(); foreach (var idb in DButton) { AccesTo acces = (from c in db.AccesToes where c.ID_Admin == AdminId && c.ID_Button == idb.ID_Button select c).SingleOrDefault <AccesTo>(); db.AccesToes.DeleteObject(acces); db.SaveChanges(); } Admin admin = (from c in db.Admins where c.Id == AdminId select c).SingleOrDefault <Admin>(); db.Admins.DeleteObject(admin); db.SaveChanges(); if (db.Admins.Where(u => u.Id == AdminId).SingleOrDefault() != null) { throw new Exception("Не удалось удалить пользователя"); } } }
/// <summary> /// Создание нового администратора /// </summary> /// <param name="SessionKey">ключ сессии</param> /// <returns></returns> public AdminDTO CreateAdmin(String SessionKey) { CheckSession(SessionKey); using (var db = new DataBase()) { var admin = new Admin(); string NewLogin; do { NewLogin = "******" + (new Random().Next()).ToString(); }while (db.Admins.Where(u => u.Login == NewLogin).Count() > 0); admin.Login = NewLogin; admin.PasswordSalt = Guid.NewGuid().ToString(); admin.Password_SaltAndHash = WCFServices.HashMd5.getMd5Hash(NewLogin + admin.PasswordSalt); admin.Email = "Не указан"; admin.AllowAccessToAdminPanel = false; admin.NotifyNewRegistrations = false; db.Admins.AddObject(admin); db.SaveChanges(); IEnumerable <AccessButtons> DButton = (from c in db.DictionaryButtons select new AccessButtons { ID = c.ID_Button }).ToList(); foreach (var idb in DButton) { AccesTo acces = new AccesTo(); acces.ID_Admin = admin.Id; acces.ID_Button = idb.ID; acces.YesNoAccess = true; db.AccesToes.AddObject(acces); db.SaveChanges(); } return(GetCollection(SessionKey, admin.Login)); } }
/// <summary> /// Добавление/удаление доступа к панелям /// </summary> /// <param name="SessionKey">номер сессии</param> /// <param name="AdminId">идентификатор администратора</param> /// <param name="access">переданные поля доступа</param> public void AccessAdmins(String SessionKey, Int32 AdminId, Boolean[] access) { CheckSession(SessionKey); using (var db = new DataBase()) { ///проверочку на пустоту не помешало бы IEnumerable <AccessToPanel> DButton = (from c in db.AccesToes where c.ID_Admin == AdminId select new AccessToPanel { ID_Button = c.ID_Button }).ToList(); Int32 i = 0; foreach (var idb in DButton) { AccesTo acces = (from c in db.AccesToes where c.ID_Admin == AdminId && c.ID_Button == idb.ID_Button select c).SingleOrDefault <AccesTo>(); acces.YesNoAccess = access[i++]; // Сохранить изменения try { db.SaveChanges(); } catch (OptimisticConcurrencyException ex) { db.Refresh(refreshMode: RefreshMode.ClientWins, collection: db.AccesToes); db.SaveChanges(); throw new Exception("Не удалось сохранить изменения. Ошибка :" + ex.Message); } } } }