Example #1
0
 public RegistrationService(IRoleService roleService,
                            IEmailService emailService,
                            INotificationService notificationService,
                            IPermanentConnectionService permanentConnectionService,
                            IUserService userService,
                            ILoginService loginService,
                            ScopedHttpContext httpContext,
                            RegistrationServiceConfiguration configuration)
 {
     _roleService                = roleService;
     _emailService               = emailService;
     _notificationService        = notificationService;
     _permanentConnectionService = permanentConnectionService;
     _userService                = userService;
     _loginService               = loginService;
     _httpContext                = httpContext.HttpContext;
     _configuration              = configuration;
 }
Example #2
0
        public static void OnInit(ClientErrorManager manager, IValidatingService validatingService, RegistrationServiceConfiguration configuration)
        {
            manager.AddErrors(new ClientErrors("Login", new Dictionary <string, ClientError>()
            {
                { "l-user-banned", new ClientError("User banned!") },
                { "l-pass-log-inc", new ClientError("Incorrect login or password!") },
                { "l-too-many-devices", new ClientError("Too many devices logged!") }
            }));

            validatingService.AddValidateFunc("date-birthday", (DateTime? prop, PropValidateContext context) => {
                if (prop == null)
                {
                    return;
                }

                if (prop.Value == null || prop.Value > DateTime.Now)
                {
                    context.Valid.Add($"[r-date-birthday-incorrect]");
                }
            });

            validatingService.AddValidateFunc("str-email-reg-no", (string prop, PropValidateContext context) => {
                if (prop == null)
                {
                    return;
                }

                if (context.SeriviceProvider.GetService <IUserService>().IsIssetByEmail(prop))
                {
                    context.Valid.Add($"[r-email-reg]"); // "User with this email is already registered!"
                }
            });

            validatingService.AddValidateFunc("str-login", (string prop, PropValidateContext ctx) => {
                if (prop == null)
                {
                    return;
                }

                if (prop.Length > configuration.MaxCountCharsLogin)
                {
                    ctx.Valid.Add($"[str-too-long, [pn-{ctx.PropName}], {configuration.MaxCountCharsLogin}]"); // "Too long login(max {_configuration.MaxCountCharsLogin} characters)!"
                }
                if (!Regex.Match(prop, "^[a-zA-Z_0-9]*$").Success)
                {
                    ctx.Valid.Add($"[str-no-spc-ch, [pn-{ctx.PropName}]]"); // "Login musn't have specials chars!"
                }
            });

            validatingService.AddValidateFunc("str-creds", (string prop, PropValidateContext ctx) => {
                if (prop == null)
                {
                    return;
                }

                if (prop.Length > configuration.MaxCountCharsName)
                {
                    ctx.Valid.Add($"[str-too-long, [pn-{ctx.PropName}], {255}]");
                }
                if (!Regex.Match(prop, "^[а-яА-ЯҐґЄєІіЇї]+$").Success)
                {
                    ctx.Valid.Add($"[str-no-spc-ch-2, [pn-{ctx.PropName}]]"); //"Name musn't have specials chars!"
                }
            });

            validatingService.AddValidateFunc("str-password", (string prop, PropValidateContext ctx) => {
                if (prop == null)
                {
                    return;
                }

                if (prop.Length > configuration.MaxCountCharsPassword)
                {
                    ctx.Valid.Add($"[str-too-long, [pn-{ctx.PropName}], {configuration.MaxCountCharsPassword}]");//$"Too long password(max{} characters)!"
                }
                if (prop.Length < configuration.MinCountCharsPassword)
                {
                    ctx.Valid.Add($"[str-too-sh, [pn-{ctx.PropName}], {configuration.MaxCountCharsPassword}]");//$"Password must have {_configuration.MinCountCharsPassword} and more chars!
                }
                if (!prop.Any(c => char.IsDigit(c)))
                {
                    ctx.Valid.Add($"[str-no-dig, [pn-{ctx.PropName}]]");//$"Password must have minimum one digit!"
                }
            });

            validatingService.AddValidateFunc("str-password-rep", (string prop, PropValidateContext ctx) => {
                if (prop == null || ctx.TypeDto.GetProperty("Password") == null ||
                    ctx.TypeDto.GetProperty("Password").GetValue(ctx.Dto) == null)
                {
                    return;
                }


                if (prop != (string)ctx.TypeDto.GetProperty("Password").GetValue(ctx.Dto))
                {
                    ctx.Valid.Add($"[str-inc-rep, [pn-{ctx.PropName}]]"); //$"Incorrect repeat password!"
                }
            });

            validatingService.AddValidateFunc("str-email", (string prop, PropValidateContext context) =>
            {
                if (prop == null)
                {
                    return;
                }

                if (!Regex.IsMatch(prop, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
                {
                    context.Valid.Add($"[v-str-email, [pn-{context.PropName}]]");
                }
            });
        }
Example #3
0
 public static void UseLoginRegistrationService(this IServiceCollection services, RegistrationServiceConfiguration configuration)
 {
     services.AddSingleton(configuration);
     services.AddScoped <ILoginService, LoginService>();
     services.AddScoped <IRegistrationService, RegistrationService>();
 }