示例#1
0
        public Result <object> DeletePolicySet(PolicySetDataContract policySet)
        {
            var result = new Result <object>();

            try
            {
                if (policySet == null)
                {
                    throw new Exception("Hабор политик не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var delPolicySet = ctx.tbl_policy_set.FirstOrDefault(x => x.id == policySet.PolicySetId);
                    ctx.tbl_policy_set.Remove(delPolicySet);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка удаления наборa политик. ", ex.Message);
            }

            return(result);
        }
示例#2
0
        public Result <object> AddUser(UserDataContract user)
        {
            var result = new Result <object>();

            try
            {
                if (user == null)
                {
                    throw new Exception("Новый пользователь не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    user.UserId = ctx.tbl_user.Any() ? ctx.tbl_user.Max(x => x.id) + 1 : 1;
                    var newUser = UserDataContract.FromUserDataContractToTblUser(user);
                    ctx.tbl_user.Add(newUser);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления пользователя. ", ex.Message);
            }

            return(result);
        }
示例#3
0
        public Result <object> SetDevicesForUpdate(List <long> loginIdList)
        {
            var result = new Result <object>();

            try
            {
                if (loginIdList == null || !loginIdList.Any())
                {
                    throw new Exception("Пустой список устройств для обновления");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    foreach (var loginDevice in ctx.tbl_login_devices.Where(x => loginIdList.Contains(x.id_login)))
                    {
                        loginDevice.need_update_device = true;
                    }

                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка сохранения списка устройств для обновления. ", ex.Message);
            }

            return(result);
        }
示例#4
0
        public string UpdateLoginDevicesRest(LoginDevicesDataContract loginDevice)
        {
            var result = new Result <object>();

            try
            {
                using (var ctx = new PolicyProjectEntities())
                {
                    var existed = ctx.tbl_login_devices.FirstOrDefault(
                        x => x.id_device == loginDevice.DeviceId && x.id_login == loginDevice.LoginId);

                    if (existed == null)
                    {
                        throw new Exception(string.Concat("Не найдено устройство с ИД ", loginDevice.DeviceId,
                                                          ", принадлежащий логину с ИД ", loginDevice.LoginId));
                    }

                    existed.need_update_device = loginDevice.NeedUpdateDevice;
                    result.BoolRes             = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка обновления статуса устройства. ", ex.Message);
            }

            return(JsonConvert.SerializeObject(result));
        }
        public Result <object> DeleteEventLog(DateTime fromDate, DateTime?toDate, int?eventId)
        {
            var result = new Result <object>();

            try
            {
                using (var ctx = new PolicyProjectEntities())
                {
                    var finalDate    = (toDate ?? DateTime.Now).AddDays(1);
                    var logForDelete = ctx.tbl_activity_log.Where(x => x.log_date >= fromDate &&
                                                                  x.log_date <= finalDate);

                    if (eventId != null)
                    {
                        logForDelete = logForDelete.Where(x => x.id_event == eventId);
                    }

                    foreach (var log in logForDelete)
                    {
                        ctx.tbl_activity_log.Remove(log);
                    }

                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка удаления лога событий. ", ex.Message);
            }

            return(result);
        }
示例#6
0
        public Result <object> DeleteUser(UserDataContract user)
        {
            var result = new Result <object>();

            try
            {
                if (user == null)
                {
                    throw new Exception("Пользователь не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var delUser = ctx.tbl_user.FirstOrDefault(x => x.id == user.UserId);
                    ctx.tbl_user.Remove(delUser);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка удаления пользователя. ", ex.Message);
            }

            return(result);
        }
示例#7
0
        public Result <object> DeleteGroup(GroupDataContract group)
        {
            var result = new Result <object>();

            try
            {
                if (group == null)
                {
                    throw new Exception("Группа не задана");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var delGroup = ctx.tbl_group.FirstOrDefault(x => x.id == group.GroupId);
                    ctx.tbl_group.Remove(delGroup);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка удаления группы. ", ex.Message);
            }

            return(result);
        }
        public Result <object> DeleteLogin(LoginDataContract login)
        {
            var result = new Result <object>();

            try
            {
                if (login == null)
                {
                    throw new Exception("Логин не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var delLogin = ctx.tbl_login.FirstOrDefault(x => x.id == login.LoginId);
                    ctx.tbl_login.Remove(delLogin);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка удаления логина. ", ex.Message);
            }

            return(result);
        }
示例#9
0
        public Result <object> AddPolicySet(PolicySetDataContract policySet)
        {
            var result = new Result <object>();

            try
            {
                if (policySet == null)
                {
                    throw new Exception("Новый набор политик не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    policySet.PolicySetId = ctx.tbl_policy_set.Any() ? ctx.tbl_policy_set.Max(x => x.id) + 1 : 1;
                    var newPolicySet = PolicySetDataContract.FromPolicySetDataContractToTblPolicySet(policySet);
                    ctx.tbl_policy_set.Add(newPolicySet);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления набора политик. ", ex.Message);
            }

            return(result);
        }
示例#10
0
        public Result <object> DeleteEvent(EventDataContract evnt)
        {
            var result = new Result <object>();

            try
            {
                if (evnt == null)
                {
                    throw new Exception("Событие не задано");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var delEvent = ctx.tbl_event.FirstOrDefault(x => x.id == evnt.EventId);
                    ctx.tbl_event.Remove(delEvent);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка удаления события. ", ex.Message);
            }

            return(result);
        }
示例#11
0
        public Result <object> DeleteDevice(DeviceInfoDataContract device)
        {
            var result = new Result <object>();

            try
            {
                if (device == null)
                {
                    throw new Exception("Yстройство не задано");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var delDevice = ctx.tbl_device_info.FirstOrDefault(x => device.DeviceId == x.id);
                    ctx.tbl_device_info.Remove(delDevice);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка удаления устройствa. ", ex.Message);
            }

            return(result);
        }
示例#12
0
        public Result <object> AddDevice(DeviceInfoDataContract device)
        {
            var result = new Result <object>();

            try
            {
                if (device == null)
                {
                    throw new Exception("Новое устройство не задано");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    device.DeviceId = ctx.tbl_device_info.Any() ? ctx.tbl_device_info.Max(x => x.id) + 1 : 1;
                    var newDevice = DeviceInfoDataContract.FromDeviceInfoDataContractToTblDeviceInfo(device);
                    ctx.tbl_device_info.Add(newDevice);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления устройствa. ", ex.Message);
            }

            return(result);
        }
        public Result <object> AddEventLog(EventLogDataContract eventLog)
        {
            var result = new Result <object>();

            try
            {
                if (eventLog == null)
                {
                    throw new Exception("Новая запись лога не задана");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    eventLog.EventLogId = ctx.tbl_activity_log.Any() ? ctx.tbl_activity_log.Max(x => x.id) + 1 : 1;
                    var newEventLog = EventLogDataContract.FromEventLogDataContractToTblEventLog(eventLog);
                    newEventLog.log_date = DateTime.Now;
                    ctx.tbl_activity_log.Add(newEventLog);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления записи лога. ", ex.Message);
            }

            return(result);
        }
示例#14
0
        public Result <object> UpdateDevice(DeviceInfoDataContract oldDevice, DeviceInfoDataContract newDevice)
        {
            var result = new Result <object>();

            try
            {
                if (oldDevice == null)
                {
                    throw new Exception("Текущее устройство не задано");
                }

                if (newDevice == null)
                {
                    throw new Exception("Новое устройство не задано");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected = ctx.tbl_device_info.FirstOrDefault(x => oldDevice.DeviceId == x.id);

                    if (selected == null)
                    {
                        throw new Exception("Текущее устройство не найдено");
                    }

                    selected.device_name          = newDevice.DeviceName;
                    selected.device_ip_addr       = newDevice.DeviceIpAddress;
                    selected.device_serial_number = newDevice.DeviceSerialNumber;
                    selected.device_mac_addr      = newDevice.DeviceMacAddress;
                    selected.device_platform_id   = newDevice.DevicePlatformId;
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка изменения устройствa. ", ex.Message);
            }

            return(result);
        }
示例#15
0
        public Result <object> UpdatePolicySet(PolicySetDataContract oldPolicySet, PolicySetDataContract newPolicySet)
        {
            var result = new Result <object>();

            try
            {
                if (oldPolicySet == null)
                {
                    throw new Exception("Текущий набор политик не задан");
                }

                if (newPolicySet == null)
                {
                    throw new Exception("Новый набор политик не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected = ctx.tbl_policy_set.FirstOrDefault(x => x.id == oldPolicySet.PolicySetId);

                    if (selected == null)
                    {
                        throw new Exception("Текущий набор политик не найден");
                    }

                    selected.id_policy    = newPolicySet.PolicyId;
                    selected.id_group     = newPolicySet.GroupId;
                    selected.id_login     = newPolicySet.LoginId;
                    selected.selected     = newPolicySet.Selected;
                    selected.policy_param = newPolicySet.PolicyParam;
                    result.BoolRes        = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка изменения наборa политик. ", ex.Message);
            }

            return(result);
        }
示例#16
0
        public Result <object> UpdateLogin(LoginDataContract oldLogin, LoginDataContract newLogin)
        {
            var result = new Result <object>();

            try
            {
                if (oldLogin == null)
                {
                    throw new Exception("Текущий логин не задан");
                }

                if (newLogin == null)
                {
                    throw new Exception("Новый логин не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected = ctx.tbl_login.FirstOrDefault(x => x.id == oldLogin.LoginId);

                    if (selected == null)
                    {
                        throw new Exception("Текущий логин не найден");
                    }

                    selected.login       = newLogin.Login;
                    selected.pwd         = newLogin.Password;
                    selected.certificate = CreateCertificate();
                    selected.id_user     = newLogin.UserId;
                    selected.id_group    = newLogin.GroupId;
                    result.BoolRes       = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка изменения логина. ", ex.Message);
            }

            return(result);
        }
示例#17
0
        public Result <object> UpdatePolicy(PolicyDataContract oldPolicy, PolicyDataContract newPolicy)
        {
            var result = new Result <object>();

            try
            {
                if (oldPolicy == null)
                {
                    throw new Exception("Текущая политика не задана");
                }

                if (newPolicy == null)
                {
                    throw new Exception("Новая политика не задана");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected = ctx.tbl_policy.FirstOrDefault(x => x.id == oldPolicy.PolicyId);

                    if (selected == null)
                    {
                        throw new Exception("Текущая политика не найдена");
                    }

                    selected.policy_name          = newPolicy.PolicyName;
                    selected.platform_id          = newPolicy.PlatformId;
                    selected.policy_instruction   = newPolicy.PolicyInstruction;
                    selected.policy_default_param = newPolicy.PolicyDefaultParam;
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка изменения политики. ", ex.Message);
            }

            return(result);
        }
示例#18
0
        public Result <object> AddPolicy(PolicyDataContract policy)
        {
            var result = new Result <object>();

            try
            {
                if (policy == null)
                {
                    throw new Exception("Новая политика не задана");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected =
                        ctx.tbl_policy.FirstOrDefault(
                            x =>
                            x.platform_id == policy.PlatformId &&
                            x.policy_name.Trim()
                            .Equals(policy.PolicyName.Trim(), StringComparison.InvariantCultureIgnoreCase));

                    if (selected != null)
                    {
                        throw new Exception("Такая политика уже существует");
                    }

                    policy.PolicyId = ctx.tbl_policy.Any() ? ctx.tbl_policy.Max(x => x.id) + 1 : 1;
                    var newPolicy = PolicyDataContract.FromPolicyDataContractToTblPolicy(policy);
                    ctx.tbl_policy.Add(newPolicy);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления политики. ", ex.Message);
            }

            return(result);
        }
示例#19
0
        public Result <object> UpdateUser(UserDataContract oldUser, UserDataContract newUser)
        {
            var result = new Result <object>();

            try
            {
                if (oldUser == null)
                {
                    throw new Exception("Текущий пользователь не задан");
                }

                if (newUser == null)
                {
                    throw new Exception("Новый пользователь не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected = ctx.tbl_user.FirstOrDefault(x => x.id == oldUser.UserId);

                    if (selected == null)
                    {
                        throw new Exception("Текущий пользователь не найден");
                    }

                    selected.first_name  = newUser.UserFirstName;
                    selected.last_name   = newUser.UserLastName;
                    selected.middle_name = newUser.UserMiddleName;
                    result.BoolRes       = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка изменения пользователя. ", ex.Message);
            }

            return(result);
        }
示例#20
0
        public Result <object> AddEvent(EventDataContract evnt)
        {
            var result = new Result <object>();

            try
            {
                if (evnt == null)
                {
                    throw new Exception("Новое событие не задано");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected =
                        ctx.tbl_event.FirstOrDefault(
                            x =>
                            x.event_name.Trim()
                            .Equals(evnt.EventName.Trim(), StringComparison.InvariantCultureIgnoreCase));

                    if (selected != null)
                    {
                        throw new Exception("Событие уже существует");
                    }

                    evnt.EventId = ctx.tbl_event.Any() ? ctx.tbl_event.Max(x => x.id) + 1 : 1;
                    var newEvent = EventDataContract.FromEventDataContractToTblEvent(evnt);
                    ctx.tbl_event.Add(newEvent);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления события. ", ex.Message);
            }

            return(result);
        }
示例#21
0
        public Result <object> AddGroup(GroupDataContract group)
        {
            var result = new Result <object>();

            try
            {
                if (group == null)
                {
                    throw new Exception("Новая группа не задана");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected =
                        ctx.tbl_group.FirstOrDefault(
                            x =>
                            x.group_name.Trim()
                            .Equals(group.GroupName.Trim(), StringComparison.InvariantCultureIgnoreCase));

                    if (selected != null)
                    {
                        throw new Exception("Группа уже существует");
                    }

                    group.GroupId = ctx.tbl_group.Any() ? ctx.tbl_group.Max(x => x.id) + 1 : 1;
                    var newGroup = GroupDataContract.FromGroupDataContractToTblGroup(group);
                    ctx.tbl_group.Add(newGroup);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления группы. ", ex.Message);
            }

            return(result);
        }
示例#22
0
        public Result <object> AddLogin(LoginDataContract login)
        {
            var result = new Result <object>();

            try
            {
                if (login == null)
                {
                    throw new Exception("Новый логин не задан");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected =
                        ctx.tbl_login.FirstOrDefault(
                            x => x.login.Trim().Equals(login.Login.Trim(), StringComparison.InvariantCultureIgnoreCase));

                    if (selected != null)
                    {
                        throw new Exception("Такой логин уже существует");
                    }

                    login.LoginId = ctx.tbl_login.Any() ? ctx.tbl_login.Max(x => x.id) + 1 : 1;
                    var newLogin = LoginDataContract.FromLoginDataContractToTblLogin(login);
                    newLogin.certificate = CreateCertificate();
                    ctx.tbl_login.Add(newLogin);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления логина. ", ex.Message);
            }

            return(result);
        }
示例#23
0
        public Result <object> UpdateEvent(EventDataContract oldEvent, EventDataContract newEvent)
        {
            var result = new Result <object>();

            try
            {
                if (oldEvent == null)
                {
                    throw new Exception("Текущее событие не задано");
                }

                if (newEvent == null)
                {
                    throw new Exception("Новое событие не задано");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected = ctx.tbl_event.FirstOrDefault(x => x.id == oldEvent.EventId);

                    if (selected == null)
                    {
                        throw new Exception("Текущее событие не найдено");
                    }

                    selected.event_name = newEvent.EventName;
                    result.BoolRes      = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка изменения события. ", ex.Message);
            }

            return(result);
        }
示例#24
0
        public Result <object> UpdateGroup(GroupDataContract oldGroup, GroupDataContract newGroup)
        {
            var result = new Result <object>();

            try
            {
                if (oldGroup == null)
                {
                    throw new Exception("Текущая группа не задана");
                }

                if (newGroup == null)
                {
                    throw new Exception("Новая группа не задана");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var selected = ctx.tbl_group.FirstOrDefault(x => x.id == oldGroup.GroupId);

                    if (selected == null)
                    {
                        throw new Exception("Текущая группа не найдена");
                    }

                    selected.group_name = newGroup.GroupName;
                    result.BoolRes      = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка изменения группы. ", ex.Message);
            }

            return(result);
        }
        public Result <object> AddEventLogExplicit(string device, string login, int eventId, long documentId,
                                                   string message)
        {
            var result = new Result <object>();

            try
            {
                if (string.IsNullOrEmpty(device.Trim()) || string.IsNullOrEmpty(login.Trim()) || eventId < 1)
                {
                    throw new Exception("Новая запись лога не задана");
                }

                using (var ctx = new PolicyProjectEntities())
                {
                    var eventLog = new EventLogDataContract
                    {
                        EventLogId = ctx.tbl_activity_log.Any() ? ctx.tbl_activity_log.Max(x => x.id) + 1 : 1,
                        EventId    = eventId,
                        DocumentId = documentId,
                        Message    = message,
                        Login      = login,
                        Device     = device
                    };
                    var newEventLog = EventLogDataContract.FromEventLogDataContractToTblEventLog(eventLog);
                    newEventLog.log_date = DateTime.Now;
                    ctx.tbl_activity_log.Add(newEventLog);
                    result.BoolRes = ctx.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка добавления записи лога. ", ex.Message);
            }

            return(result);
        }
示例#26
0
        public Result <object> UpdatePolicySetForLogin(long loginId, PolicySetDataContract[] policySetList)
        {
            var result = new Result <object>();

            using (var transaction = new TransactionScope())
            {
                try
                {
                    if (loginId < 1)
                    {
                        throw new Exception("Не выбран логин");
                    }

                    using (var ctx = new PolicyProjectEntities())
                    {
                        var  groupId                = ctx.tbl_login.FirstOrDefault(x => x.id == loginId).id_group;
                        var  groupPolicySet         = ctx.tbl_policy_set.Where(x => x.id_group == groupId);
                        var  policySetForLoginInTbl = ctx.tbl_policy_set.Where(x => x.id_login == loginId);
                        long newPolicySetId         = 0;

                        for (var i = 0; i < policySetList.Length; i++)
                        {
                            var containedGroup =
                                groupPolicySet.FirstOrDefault(x => x.id_policy == policySetList[i].PolicyId);
                            var containedInTbl =
                                policySetForLoginInTbl.FirstOrDefault(x => x.id_policy == policySetList[i].PolicyId);

                            if (policySetList[i].Selected.HasValue && policySetList[i].Selected.Value)
                            {
                                if (containedGroup == null)
                                {
                                    if (containedInTbl != null)
                                    {
                                        containedInTbl.id_policy    = policySetList[i].PolicyId;
                                        containedInTbl.id_login     = policySetList[i].LoginId;
                                        containedInTbl.selected     = policySetList[i].Selected;
                                        containedInTbl.policy_param = policySetList[i].PolicyParam;
                                    }
                                    else
                                    {
                                        if (newPolicySetId == 0)
                                        {
                                            newPolicySetId = ctx.tbl_policy_set.Any()
                                                ? ctx.tbl_policy_set.Max(x => x.id) + 1
                                                : 1;
                                        }
                                        else
                                        {
                                            newPolicySetId++;
                                        }

                                        var newPolicySet = new tbl_policy_set
                                        {
                                            id           = newPolicySetId,
                                            id_policy    = policySetList[i].PolicyId,
                                            id_login     = policySetList[i].LoginId,
                                            selected     = policySetList[i].Selected,
                                            policy_param = policySetList[i].PolicyParam
                                        };
                                        ctx.tbl_policy_set.Add(newPolicySet);
                                    }
                                }
                                else
                                {
                                    if (containedInTbl == null)
                                    {
                                        continue;
                                    }

                                    var deletedPolicySet =
                                        ctx.tbl_policy_set.FirstOrDefault(x => x.id == containedInTbl.id);
                                    ctx.tbl_policy_set.Remove(deletedPolicySet);
                                }
                            }
                            else
                            {
                                if (containedGroup == null)
                                {
                                    if (containedInTbl == null)
                                    {
                                        continue;
                                    }

                                    var deletedPolicySet =
                                        ctx.tbl_policy_set.FirstOrDefault(x => x.id == containedInTbl.id);
                                    ctx.tbl_policy_set.Remove(deletedPolicySet);
                                }
                                else
                                {
                                    if (containedInTbl != null)
                                    {
                                        containedInTbl.id_policy    = policySetList[i].PolicyId;
                                        containedInTbl.id_login     = policySetList[i].LoginId;
                                        containedInTbl.selected     = policySetList[i].Selected;
                                        containedInTbl.policy_param = policySetList[i].PolicyParam;
                                    }
                                    else
                                    {
                                        if (newPolicySetId == 0)
                                        {
                                            newPolicySetId = ctx.tbl_policy_set.Any()
                                                ? ctx.tbl_policy_set.Max(x => x.id) + 1
                                                : 1;
                                        }
                                        else
                                        {
                                            newPolicySetId++;
                                        }

                                        var newPolicySet = new tbl_policy_set
                                        {
                                            id           = newPolicySetId,
                                            id_policy    = policySetList[i].PolicyId,
                                            id_login     = policySetList[i].LoginId,
                                            selected     = policySetList[i].Selected,
                                            policy_param = policySetList[i].PolicyParam
                                        };
                                        ctx.tbl_policy_set.Add(newPolicySet);
                                    }
                                }
                            }
                        }

                        result.BoolRes = ctx.SaveChanges() == policySetList.Length;

                        if (result.BoolRes)
                        {
                            transaction.Complete();
                        }
                    }
                }
                catch (Exception ex)
                {
                    transaction.Dispose();
                    result.BoolRes  = false;
                    result.ErrorRes = string.Concat("Ошибка сохранения набора политик логина. ", ex.Message);
                }
            }

            return(result);
        }
示例#27
0
        public Result <object> UpdateLoginDevices(long loginId, LoginDevicesDataContract[] loginDevicesList)
        {
            var result = new Result <object>();

            using (var transaction = new TransactionScope())
            {
                try
                {
                    using (var ctx = new PolicyProjectEntities())
                    {
                        var deletedList = new List <tbl_login_devices>();

                        foreach (var loginDevice in ctx.tbl_login_devices.Where(x => x.id_login == loginId))
                        {
                            if (loginDevicesList.All(x => x.DeviceId != loginDevice.id_device))
                            {
                                deletedList.Add(loginDevice);
                            }
                        }

                        foreach (var forDeletion in deletedList)
                        {
                            ctx.tbl_login_devices.Remove(forDeletion);
                        }

                        long newLoginDeviceId = 0;

                        foreach (var selectedDevice in loginDevicesList)
                        {
                            if (
                                ctx.tbl_login_devices.Any(
                                    x => x.id_device == selectedDevice.DeviceId && x.id_login == selectedDevice.LoginId))
                            {
                                continue;
                            }

                            if (newLoginDeviceId == 0)
                            {
                                newLoginDeviceId = ctx.tbl_login_devices.Any()
                                    ? ctx.tbl_login_devices.Max(x => x.id) + 1
                                    : 1;
                            }
                            else
                            {
                                newLoginDeviceId++;
                            }

                            ctx.tbl_login_devices.Add(new tbl_login_devices
                            {
                                id                 = newLoginDeviceId,
                                id_device          = selectedDevice.DeviceId,
                                id_login           = selectedDevice.LoginId,
                                need_update_device = selectedDevice.NeedUpdateDevice
                            });
                        }

                        result.BoolRes = ctx.SaveChanges() > 0;

                        if (result.BoolRes)
                        {
                            transaction.Complete();
                        }
                    }
                }
                catch (Exception ex)
                {
                    transaction.Dispose();
                    result.BoolRes  = false;
                    result.ErrorRes = string.Concat("Ошибка сохранения списка устройств логина. ", ex.Message);
                }
            }

            return(result);
        }