Пример #1
0
        public async Task <ResultCrmDb> AddRoleForLevelOrgAsync(RoleCreateArgs args)
        {
            var result = new ResultCrmDb();

            try
            {
                if ((await _crmDbContext.userrole.FirstOrDefaultAsync(r => r.LevelOrgId == args.IdLevelorg & r.Name == args.CaptionRole)) != null)
                {
                    result.AddError("", "Такая роль уже существует!");
                    return(result);
                }
                var role = new userrole();
                role.LevelOrgId = args.IdLevelorg;
                role.Name       = args.CaptionRole;
                role.Sysname    = args.CaptionRole;
                await _crmDbContext.userrole.AddAsync(role);

                await _crmDbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                result.AddError("", e.Message);
            }
            return(result);
        }
Пример #2
0
        public async Task <ResultCrmDb> EditAccessAsync(int idRole, ItemAccess[] fieldsArgs)
        {
            var result = new ResultCrmDb();

            try
            {
                foreach (var field in fieldsArgs.Where(x => x.IsField == true))
                {
                    var updated = await _crmDbContext.accessForm.FirstOrDefaultAsync(x => x.UserRoleId == idRole& x.FormModelId == field.FormId& x.FieldId == field.FieldId);

                    if (updated != null)
                    {
                        updated.TypeAccec = field.typeAccess;
                        _crmDbContext.accessForm.Update(updated);
                    }
                    else
                    {
                        var access = new AccessForm()
                        {
                            FieldId     = field.FieldId,
                            UserRoleId  = idRole,
                            FormModelId = field.FormId,
                            TypeAccec   = field.typeAccess
                        };
                        await _crmDbContext.accessForm.AddAsync(access);
                    }
                }
                await _crmDbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                result.AddError("", e.Message);
            }
            return(result);
        }
        public async Task <ResultCrmDb> AddAsync(LevelOrgAddArgs args)
        {
            var result = new ResultCrmDb();
            var toAdd  = new t_levelorg();

            try
            {
                var org = await _crmDbContext.t_levelorg.FirstOrDefaultAsync(r => r.Inn == args.Inn& r.Kpp == args.Kpp& r.Oktmo == args.Oktmo);

                if (org != null)
                {
                    result.AddError("", "Организация с такими ИНН, КПП, ОКТМО уже существует!");
                    return(result);
                }
                var l = _crmDbContext.t_levelorg.Count();
                toAdd = args;
                if (toAdd.Parent == null || toAdd.Parent == 0)
                {
                    if (await _crmDbContext.t_levelorg.FirstOrDefaultAsync(f => f.Parent == null || f.Parent == 0) != null)
                    {
                        throw new Exception("Не возможно иметь 2 основных уровня levelorg");
                    }
                    else
                    {
                        if (_crmDbContext.t_levelorg.Count() != 0)
                        {
                            if (await _crmDbContext.t_levelorg.FirstOrDefaultAsync(f => toAdd.Parent.ToString() == f.Sysid.ToString()) == null)
                            {
                                throw new Exception("Не существует родительской записи для levelorg");
                            }
                        }
                    }
                }
                await _crmDbContext.t_levelorg.AddAsync(toAdd);

                await _crmDbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                result.AddError("", e.Message);
            }
            return(result);
        }
Пример #4
0
        public async Task <ResultCrmDb> EditAccessAsync(int idRole, ItemAccess[] items)
        {
            var result = new ResultCrmDb();

            try
            {
                var it = items.Where(x => x.IsForm == true);
                var ac = _crmDbContext.accessForm.ToList();

                foreach (var forms in items.Where(x => x.IsForm == true))
                {
                    var updated = await _crmDbContext.accessForm.Where(x => x.UserRoleId == idRole).Where(x => x.ButtonFormId == null).Where(x => x.FieldId == null).FirstOrDefaultAsync(x => x.FormModelId == forms.FormId);

                    var updated1 = _crmDbContext.accessForm.Where(x => x.UserRoleId == idRole & (x.FormModelId == forms.FormId) & (x.ButtonFormId == null) & (x.FieldId == null)).FirstOrDefault();
                    if (updated != null)
                    {
                        updated.TypeAccec = forms.typeAccess;
                        _crmDbContext.accessForm.Update(updated);
                        await _crmDbContext.SaveChangesAsync();
                    }
                    else
                    {
                        var access = new AccessForm()
                        {
                            UserRoleId  = idRole,
                            FormModelId = forms.FormId,
                            TypeAccec   = forms.typeAccess
                        };
                        await _crmDbContext.accessForm.AddAsync(access);

                        await _crmDbContext.SaveChangesAsync();
                    }
                }
            }
            catch (Exception e)
            {
                result.AddError("", e.Message);
            }
            return(result);
        }
Пример #5
0
        public async Task <ResultCrmDb> RegisterUserAsync(RegistrationUserArgs args)
        {
            var           result     = new ResultCrmDb();
            var           errors     = 0;
            List <string> textErrors = new List <string>();

            try
            {
                if (string.IsNullOrWhiteSpace(args.Account))
                {
                    textErrors.Add("аккаунт пользователя не указан");
                    errors++;
                }
                if (string.IsNullOrWhiteSpace(args.Surname))
                {
                    textErrors.Add("Фамилия пользователя не указана");
                    errors++;
                }
                if (errors > 0)
                {
                    throw new Exception();
                }
                var countUser = await _crmDbContext.User.CountAsync(c => c.Account.ToLower() == args.Account.ToLower());

                if (countUser > 0)
                {
                    textErrors.Add("пользователь с таким логином уже существует");
                    errors++;
                }
                var user = new User();
                user.Account    = args.Account;
                user.Email      = args.Email;
                user.Phone      = args.Phone;
                user.Name       = args.Name;
                user.Surname    = args.Surname;
                user.Middlename = args.Middlename;
                await _crmDbContext.user.AddAsync(user);

                await _crmDbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                var err = "";

                if (errors > 0)
                {
                    err = string.Join(",\n", textErrors.ToArray());
                }
                else
                {
                    err = e.Message;
                }
                var patternError = $"Ошибка регистрации пользователя:\n {err}.";
                result.AddError("", $"{patternError}");
            }
            return(result);
        }