Esempio n. 1
0
        public LoginModule(ISettingsService <PlexRequestSettings> pr, ICustomUserMapper m, IResourceLinker linker, IRepository <UserLogins> userLoginRepo, ISecurityExtensions security)
            : base(pr, security)
        {
            UserMapper = m;
            Get["LocalLogin", "/login"] = _ =>
            {
                if (LoggedIn)
                {
                    var url = linker.BuildRelativeUri(Context, "SearchIndex");
                    return(Response.AsRedirect(url.ToString()));
                }
                dynamic model = new ExpandoObject();
                model.Redirect = Request.Query.redirect.Value ?? string.Empty;
                model.Errored  = Request.Query.error.HasValue;
                var adminCreated = UserMapper.DoUsersExist();
                model.AdminExists = adminCreated;
                return(View["Index", model]);
            };

            Get["/logout"] = x =>
            {
                if (Session[SessionKeys.UsernameKey] != null)
                {
                    Session.Delete(SessionKeys.UsernameKey);
                }
                return(CustomModuleExtensions.LogoutAndRedirect(this, !string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/" : "~/"));
            };

            Post["/login"] = x =>
            {
                var username = (string)Request.Form.Username;
                var password = (string)Request.Form.Password;
                var dtOffset = (int)Request.Form.DateTimeOffset;
                var redirect = (string)Request.Form.Redirect;

                var userId = UserMapper.ValidateUser(username, password);

                if (userId == null)
                {
                    return
                        (Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl)
                            ? $"~/{BaseUrl}/login?error=true&username="******"~/login?error=true&username="******"userlogin"))
                {
                    redirect = !string.IsNullOrEmpty(BaseUrl) ? $"/{BaseUrl}/search" : "/search";
                }

                userLoginRepo.Insert(new UserLogins
                {
                    LastLoggedIn = DateTime.UtcNow,
                    Type         = UserType.LocalUser,
                    UserId       = userId.ToString()
                });

                return(CustomModuleExtensions.LoginAndRedirect(this, userId.Value, expiry, redirect));
            };

            Get["/register"] = x =>
            {
                {
                    dynamic model = new ExpandoObject();
                    model.Errored = Request.Query.error.HasValue;

                    return(View["Register", model]);
                }
            };

            Post["/register"] = x =>
            {
                var username = (string)Request.Form.Username;
                var exists   = UserMapper.DoUsersExist();
                if (exists)
                {
                    return
                        (Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl)
                            ? $"~/{BaseUrl}/register?error=true"
                            : "~/register?error=true"));
                }
                var userId = UserMapper.CreateUser(username, Request.Form.Password, EnumHelper <Permissions> .All(), 0);
                Session[SessionKeys.UsernameKey] = username;
                return(CustomModuleExtensions.LoginAndRedirect(this, (Guid)userId));
            };

            Get["/changepassword"]  = _ => ChangePassword();
            Post["/changepassword"] = _ => ChangePasswordPost();
        }
Esempio n. 2
0
        public LoginModule(ISettingsService <PlexRequestSettings> pr, ICustomUserMapper m) : base(pr)
        {
            UserMapper    = m;
            Get["/login"] = _ =>
            {
                {
                    dynamic model = new ExpandoObject();
                    model.Redirect = Request.Query.redirect.Value ?? string.Empty;
                    model.Errored  = Request.Query.error.HasValue;
                    var adminCreated = UserMapper.DoUsersExist();
                    model.AdminExists = adminCreated;
                    return(View["Index", model]);
                }
            };

            Get["/logout"] = x => this.LogoutAndRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/" : "~/");

            Post["/login"] = x =>
            {
                var username = (string)Request.Form.Username;
                var password = (string)Request.Form.Password;
                var dtOffset = (int)Request.Form.DateTimeOffset;
                var redirect = (string)Request.Form.Redirect;

                var userId = UserMapper.ValidateUser(username, password);

                if (userId == null)
                {
                    return(Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/login?error=true&username="******"~/login?error=true&username="******"userlogin"))
                {
                    redirect = !string.IsNullOrEmpty(BaseUrl) ? $"/{BaseUrl}/search" : "/search";
                }
                return(this.LoginAndRedirect(userId.Value, expiry, redirect));
            };

            Get["/register"] = x =>
            {
                {
                    dynamic model = new ExpandoObject();
                    model.Errored = Request.Query.error.HasValue;

                    return(View["Register", model]);
                }
            };

            Post["/register"] = x =>
            {
                var username = (string)Request.Form.Username;
                var exists   = UserMapper.DoUsersExist();
                if (exists)
                {
                    return(Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/register?error=true" : "~/register?error=true"));
                }
                var userId = UserMapper.CreateUser(username, Request.Form.Password, new[] { "Admin" });
                Session[SessionKeys.UsernameKey] = username;
                return(this.LoginAndRedirect((Guid)userId));
            };

            Get["/changepassword"]  = _ => ChangePassword();
            Post["/changepassword"] = _ => ChangePasswordPost();
        }