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);
        }
Beispiel #2
0
        public static tbl_login FromLoginDataContractToTblLogin(LoginDataContract loginContractData)
        {
            if (loginContractData.LoginId < 1 || loginContractData.UserId < 1 || loginContractData.GroupId < 1)
            {
                return(null);
            }

            return(new tbl_login
            {
                id = loginContractData.LoginId,
                login = loginContractData.Login,
                pwd = loginContractData.Password,
                certificate = loginContractData.Certificate,
                id_user = loginContractData.UserId,
                id_group = loginContractData.GroupId
            });
        }
        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);
        }
Beispiel #4
0
        public static bool Compare(LoginDataContract obj1, LoginDataContract obj2)
        {
            if (obj1 == null && obj2 == null)
            {
                return(true);
            }

            if (obj1 == null && obj2 != null)
            {
                return(false);
            }

            if (obj1 != null && obj2 == null)
            {
                return(false);
            }

            return(obj1.LoginId == obj2.LoginId && obj1.UserId == obj2.UserId && obj1.GroupId == obj2.GroupId
                   &&
                   string.Equals(obj1.Certificate.Trim(), obj2.Certificate.Trim(),
                                 StringComparison.CurrentCultureIgnoreCase) &&
                   string.Equals(obj1.Login.Trim(), obj2.Login.Trim(), StringComparison.CurrentCultureIgnoreCase));
        }
        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);
        }
        public string GetLoginRest(LoginDataContract loginFilter)
        {
            var queryResult = GetLogin(loginFilter);

            return(JsonConvert.SerializeObject(queryResult));
        }
        public Result <LoginDataContract[]> GetLogin(LoginDataContract loginFilter)
        {
            var result = new Result <LoginDataContract[]>();

            try
            {
                using (var ctx = new PolicyProjectEntities())
                {
                    if (loginFilter == null)
                    {
                        result.SomeResult =
                            ctx.tbl_login.Select(
                                loginTblData => new LoginDataContract
                        {
                            LoginId     = loginTblData.id,
                            Login       = loginTblData.login,
                            Password    = loginTblData.pwd,
                            Certificate = loginTblData.certificate,
                            UserId      = loginTblData.id_user,
                            GroupId     = loginTblData.id_group,
                            GroupName   =
                                ctx.tbl_group.FirstOrDefault(group => group.id == loginTblData.id_group)
                                .group_name,
                            UserName =
                                ctx.tbl_user.Where(user => user.id == loginTblData.id_user)
                                .Select(
                                    x => (x.first_name + " " + x.last_name + " " + x.middle_name).Trim())
                                .FirstOrDefault()
                        })
                            .ToArray();
                    }
                    else
                    {
                        result.SomeResult = ctx.tbl_login.Where(x => x.id == loginFilter.LoginId
                                                                ||
                                                                x.login.Trim()
                                                                .Equals(loginFilter.Login.Trim(),
                                                                        StringComparison.InvariantCultureIgnoreCase))
                                            .Select(loginTblData => new LoginDataContract
                        {
                            LoginId     = loginTblData.id,
                            Login       = loginTblData.login,
                            Password    = loginTblData.pwd,
                            Certificate = loginTblData.certificate,
                            UserId      = loginTblData.id_user,
                            GroupId     = loginTblData.id_group,
                            GroupName   =
                                ctx.tbl_group.FirstOrDefault(group => group.id == loginTblData.id_group).group_name,
                            UserName =
                                ctx.tbl_user.Where(user => user.id == loginTblData.id_user)
                                .Select(x => (x.first_name + " " + x.last_name + " " + x.middle_name).Trim())
                                .FirstOrDefault()
                        })
                                            .ToArray();
                    }

                    result.BoolRes = true;
                }
            }
            catch (Exception ex)
            {
                result.BoolRes  = false;
                result.ErrorRes = string.Concat("Ошибка получения списка логинов. ", ex.Message);
            }

            return(result);
        }