예제 #1
0
        public ActionResult RenameExternalSource(string id, string newname)
        {
            foreach (MembershipUser user in Membership.GetAllUsers())
            {
                KcsarUserProfile profile = ProfileBase.Create(user.UserName) as KcsarUserProfile;

                List <string> external = profile.ExternalSource.Split(',').Select(f => f.Trim()).ToList();

                int idx = external.IndexOf(id);
                if (idx >= 0)
                {
                    if (string.IsNullOrWhiteSpace(newname))
                    {
                        external.Remove(id);
                    }
                    else
                    {
                        external[idx] = newname;
                    }
                    profile.ExternalSource = string.Join(",", external.ToArray());
                    profile.Save();
                }
            }

            return(new ContentResult {
                Content = "Done"
            });
        }
예제 #2
0
        public static Guid?UsernameToMemberKey(string name)
        {
            KcsarUserProfile profile = ProfileBase.Create(name) as KcsarUserProfile;

            if (profile.UsesLink)
            {
                return(new Guid(profile.LinkKey));
            }
            return(null);
        }
예제 #3
0
        //    [RequireHttps]
        public ActionResult Login(string username, string password, bool rememberMe, string returnUrl, int?id, int?p)
        {
            ViewData["PageTitle"] = "Login";

            // Basic parameter validation
            if (String.IsNullOrEmpty(username))
            {
                ModelState.AddModelError("username", "You must specify a username.");
            }
            if (String.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("password", "You must specify a password.");
            }

            if (ViewData.ModelState.IsValid)
            {
                // Attempt to login
                bool loginSuccessful = Provider.ValidateUser(username, password);

                if (loginSuccessful)
                {
                    FormsAuth.SetAuthCookie(username, rememberMe);
                    if (p != null)
                    {
                        return(RedirectToAction("GetTicket", new { p = p, returnUrl = returnUrl }));
                    }

                    if (id.HasValue)
                    {
                        return(Redirect(string.Format("{0}://{1}:{2}/{3}", this.Request.Url.Scheme, this.Request.Url.Host, id, returnUrl)));
                    }
                    else if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    else if (Roles.IsUserInRole(username, api.AccountController.APPLICANT_ROLE))
                    {
                        KcsarUserProfile profile = ProfileBase.Create(username) as KcsarUserProfile;
                        if (!string.IsNullOrWhiteSpace(profile.LinkKey))
                        {
                            return(RedirectToAction("Detail", "Members", new { id = profile.LinkKey }));
                        }
                    }
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("_FORM", "The username or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["rememberMe"] = rememberMe;
            return(View());
        }
예제 #4
0
        public ActionResult SetExternalSources(string id, string sources)
        {
            KcsarUserProfile profile = ProfileBase.Create(id) as KcsarUserProfile;

            if (profile == null)
            {
                return new ContentResult {
                           Content = "No profile"
                }
            }
            ;

            profile.ExternalSource = sources;
            profile.Save();
            return(new ContentResult {
                Content = profile.ExternalSource
            });
        }
예제 #5
0
        private static AccountListRow GetAccountView(MembershipUser account)
        {
            KcsarUserProfile profile = ProfileBase.Create(account.UserName) as KcsarUserProfile;

            AccountListRow row = new AccountListRow
            {
                Username        = account.UserName,
                LastActive      = account.LastLoginDate,
                FirstName       = profile.FirstName,
                LastName        = profile.LastName,
                LinkKey         = profile.LinkKey,
                Email           = account.Email,
                ExternalSources = profile.ExternalSource,
                IsLocked        = account.IsLockedOut,
            };

            return(row);
        }
예제 #6
0
        public ActionResult CreateUser(string first, string last, string username, string email)
        {
            if (string.IsNullOrEmpty(first))
            {
                ModelState.AddModelError("first", "First name is required");
            }

            if (string.IsNullOrEmpty(username))
            {
                ModelState.AddModelError("username", "Username is required");
            }
            else if (Membership.GetUser(username) != null)
            {
                ModelState.AddModelError("username", "Username is already taken");
            }

            if (string.IsNullOrEmpty(email))
            {
                ModelState.AddModelError("email", "Email is required");
            }

            if (ModelState.IsValid)
            {
                string password = Membership.GeneratePassword(10, 3);
                Membership.CreateUser(username, password, email);


                KcsarUserProfile profile = ProfileBase.Create(username) as KcsarUserProfile;
                if (profile != null)
                {
                    profile.FirstName = first;
                    profile.LastName  = last;
                    profile.Save();
                }

                string newUserSubject  = string.Format("New {0} User", ConfigurationManager.AppSettings["dbNameShort"] ?? "KCSARA");
                string newUserTemplate = (ConfigurationManager.AppSettings["emailNewUserBody"] ?? "Account has been created.\nUsername: {0}\nPassword: {1}").Replace("\\n", "\n");
                string newUserBody     = string.Format(newUserTemplate, username, password);
                this.SendMail(email, newUserSubject, newUserBody);

                return(RedirectToAction("Accounts"));
            }
            return(View());
        }
예제 #7
0
        public ContentResult LinkAccount()
        {
            string acct = Request["acct"].ToLower();
            Guid   id   = new Guid(Request["id"]);

            KcsarUserProfile profile = ProfileBase.Create(acct) as KcsarUserProfile;

            profile.LinkKey = id.ToString();
            profile.Save();
            Member m = this.db.Members.Where(x => x.Id == id).FirstOrDefault();

            if (m != null && string.IsNullOrWhiteSpace(m.Username))
            {
                m.Username = acct;
                this.db.SaveChanges();
            }
            return(new ContentResult {
                Content = "OK", ContentType = "text/plain"
            });
        }
예제 #8
0
        public string Signup(AccountSignup data)
        {
            if (string.IsNullOrWhiteSpace(data.Firstname))
            {
                return(string.Format(WebStrings.Validation_Required, WebStrings.Property_Firstname));
            }
            if (string.IsNullOrWhiteSpace(data.Lastname))
            {
                return(string.Format(WebStrings.Validation_Required, WebStrings.Property_Lastname));
            }

            if (string.IsNullOrWhiteSpace(data.Email))
            {
                return(string.Format(WebStrings.Validation_Required, WebStrings.Property_Email));
            }
            if (!Regex.IsMatch(data.Email, @"^\S+@\S+(\.\S+)+$"))
            {
                return(WebStrings.Validation_BadEmail);
            }

            if (data.BirthDate.HasValue == false)
            {
                return(string.Format(WebStrings.Validation_Required, WebStrings.Property_BirthDate));
            }
            if (data.BirthDate > DateTime.Today.AddYears(-APPLICANT_MIN_AGE))
            {
                return(string.Format(WebStrings.Validation_ApplicantYoung, APPLICANT_MIN_AGE));
            }
            if (data.BirthDate < DateTime.Today.AddYears(-120))
            {
                return(string.Format(WebStrings.Validation_Invalid, WebStrings.Property_BirthDate.ToLower()));
            }

            if (!(new[] { "m", "f", null }.Contains(data.Gender)))
            {
                return(string.Format(WebStrings.Validation_Invalid, WebStrings.Property_Gender.ToLower()));
            }

            if (data.Units.Length == 0)
            {
                return(string.Format(WebStrings.Validation_AtLeastOne, WebStrings.Object_Unit.ToLower()));
            }

            if (string.IsNullOrWhiteSpace(data.Username))
            {
                return(string.Format(WebStrings.Validation_Required, WebStrings.Property_Username));
            }
            if (data.Username.Length < USERNAME_MIN_LENGTH)
            {
                return(string.Format(WebStrings.Validation_MinCharacters, WebStrings.Property_Username, USERNAME_MIN_LENGTH));
            }
            if (data.Username.Length > USERNAME_MAX_LENGTH)
            {
                return(string.Format(WebStrings.Validation_MaxCharacters, WebStrings.Property_Username, USERNAME_MAX_LENGTH));
            }
            if (!Regex.IsMatch(data.Username, @"^[a-zA-Z0-9\.\-_]+$"))
            {
                return(WebStrings.Validation_UsernameFormat);
            }
            if (this.permissions.GetUser(data.Username) != null)
            {
                return(WebStrings.Validation_UsernameTaken);
            }


            if (string.IsNullOrWhiteSpace(data.Password))
            {
                return(string.Format(WebStrings.Validation_Required, WebStrings.Property_Password));
            }
            if (data.Password.Length < PASSWORD_MIN_LENGTH)
            {
                return(string.Format(WebStrings.Validation_MinCharacters, WebStrings.Property_Password, PASSWORD_MIN_LENGTH));
            }
            if (data.Password.Length > PASSWORD_MAX_LENGTH)
            {
                return(string.Format(WebStrings.Validation_MaxCharacters, WebStrings.Property_Password, PASSWORD_MAX_LENGTH));
            }


            var user = this.permissions.CreateUser(data.Username, data.Password, data.Email);

            try
            {
                user.IsApproved = false;
                this.permissions.UpdateUser(user);

                this.permissions.SetCurrentUser(data.Username);

                Member newMember = new Member
                {
                    FirstName      = data.Firstname,
                    MiddleName     = data.Middlename,
                    LastName       = data.Lastname,
                    BirthDate      = data.BirthDate,
                    InternalGender = data.Gender,
                    Status         = MemberStatus.Applicant,
                    Username       = data.Username
                };
                db.Members.Add(newMember);

                PersonContact email = new PersonContact
                {
                    Person   = newMember,
                    Type     = "email",
                    Value    = data.Email,
                    Priority = 0
                };
                db.PersonContact.Add(email);

                foreach (Guid unitId in data.Units)
                {
                    UnitsController.RegisterApplication(db, unitId, newMember);
                }

                KcsarUserProfile profile = this.permissions.GetProfile(data.Username);
                if (profile != null)
                {
                    profile.FirstName = data.Firstname;
                    profile.LastName  = data.Lastname;
                    profile.LinkKey   = newMember.Id.ToString();
                    profile.Save();
                }

                if (!this.permissions.RoleExists(APPLICANT_ROLE))
                {
                    this.permissions.CreateRole(APPLICANT_ROLE);
                }
                this.permissions.AddUserToRole(data.Username, APPLICANT_ROLE);

                string mailSubject  = string.Format(MAIL_SUBJECT_TEMPLATE, WebStrings.DatabaseName);
                string mailTemplate = this.hosting.ReadFile("EmailTemplates\\new-account-verification.html");
                string mailBody     = mailTemplate
                                      .Replace("%Username%", data.Username)
                                      .Replace("%VerifyLink%", this.hosting.GetApiUrl("Account", "Verify", data.Username, true) + "?key=" + user.ProviderUserKey.ToString())
                                      .Replace("%WebsiteContact%", this.hosting.FeedbackAddress);

                db.SaveChanges();
                this.email.SendMail(data.Email, mailSubject, mailBody);
            }
            catch (Exception ex)
            {
                log.Error(LOG_ERROR_CREATING_ACCOUNT, ex);
                this.permissions.DeleteUser(data.Username);
                return(LOG_ERROR_CREATING_ACCOUNT_EXTERNAL);
            }

            return("OK");
        }
예제 #9
0
        public ActionResult EditUser(string id, FormCollection fields)
        {
            AccountListRow row = GetAccountView(id);

            TryUpdateModel(row, new[] { "Email", "LinkKey", "ExternalSources" });

            if (string.IsNullOrWhiteSpace(row.LinkKey))
            {
                TryUpdateModel(row, new[] { "LastName", "FirstName" });
            }

            if (row.LastName != fields["LastName"])
            {
                ModelState.SetModelValue("LastName", new ValueProviderResult(fields["LastName"], fields["LastName"], CultureInfo.CurrentUICulture));
                ModelState.AddModelError("LastName", "Can't be changed while Link is set");
            }

            if (row.FirstName != fields["FirstName"])
            {
                ModelState.SetModelValue("FirstName", new ValueProviderResult(fields["FirstName"], fields["FirstName"], CultureInfo.CurrentUICulture));
                ModelState.AddModelError("FirstName", "Can't be changed while Link is set");
            }

            if (ModelState.IsValid)
            {
                MembershipUser   user    = Membership.GetUser(id);
                KcsarUserProfile profile = ProfileBase.Create(user.UserName) as KcsarUserProfile;

                if (user.Email != row.Email)
                {
                    user.Email = row.Email;
                }

                if (profile.LastName != row.LastName)
                {
                    profile.LastName = row.LastName;
                }
                if (profile.FirstName != row.FirstName)
                {
                    profile.FirstName = row.FirstName;
                }
                if (profile.LinkKey != row.LinkKey)
                {
                    profile.LinkKey = row.LinkKey;
                }
                if (profile.ExternalSource != row.ExternalSources)
                {
                    profile.ExternalSource = row.ExternalSources;
                }

                try
                {
                    Membership.UpdateUser(user);
                    profile.Save();
                    ViewData["success"] = "Saved OK";
                }
                catch (Exception e)
                {
                    ViewData["error"] = e.ToString();
                }
            }

            return(View(row));
            //TryUpdateModel(user, new[] { "Email" });



            //ModelState.SetModelValue("Owners", fields["Owners"]);
            //role.Owners.Clear();
            //role.Owners.AddRange((fields["Owners"] ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(f => new Guid(f.Trim())));

            //ModelState.SetModelValue("Destinations", fields["Destinations"]);
            //role.Destinations.Clear();
            //role.Destinations.AddRange((fields["Destinations"] ?? "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()));

            //try
            //{
            //    nested.UpdateRole(role);
            //    ViewData["success"] = "Saved OK";
            //}
            //catch (Exception e)
            //{
            //    ViewData["error"] = e.ToString();
            //}

            //return View(role);
        }