Ejemplo n.º 1
0
        public ActionResult Create(SMR.KM.Business.Models.User user)
        {
            try
            {
                if (_log.IsInfoEnabled)
                {
                    _log.Info("Calling AddUser method of UserService.");
                }
                if (string.IsNullOrEmpty(user.UserAvatar))
                {
                    ModelState.Remove("UserAvatar");
                    user.UserAvatar = Url.Content("~/Content/Images/Anonymsuser.jpg");
                }
                // bool canCreateProfile = _permissionServices.CanCreateProfile(CurrentUser, "Create", "User");
                //if (!canCreateProfile)
                //{
                //    throw new Exception("You are not authorized to create user.");
                //}
                if (ModelState.IsValid)
                {
                    MembershipCreateStatus createStatus = MembershipService.CreateUser(user.UserName, user.Password, user.Email, user.PlantId);
                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        var item = _userServices.UserIdByUserName(user.UserName);
                        _userServices.AddUser(user, CurrentUser);

                        string message = item.IsAnonymous ? "use the password you registered with" : "use your windows logon password";
                        _emailServices.RegisteredUserEmail(user.Email, string.IsNullOrEmpty(user.FirstName) && string.IsNullOrEmpty(user.LastName) ? user.UserName : string.IsNullOrEmpty(user.FirstName) ? user.LastName : string.IsNullOrEmpty(user.LastName) ? user.FirstName : user.FirstName.ToUpper() == user.LastName.ToUpper() ? user.FirstName.ToUpper() :user.LastName.ToUpper() + " " + user.FirstName.ToUpper(), user.UserId, message);
                        return(RedirectToAction("Index", "User"));
                    }
                    else
                    {
                        ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                    }
                }
            }
            catch (DataException ex)
            {
                if (_log.IsErrorEnabled)
                {
                    _log.Error(ex.Message, ex);
                }
                throw ex;
            }
            user.Plants     = _userServices.GetPlant();
            user.UserGroups = _userServices.GetUserGroups();
            return(View(user));
        }
Ejemplo n.º 2
0
        public ActionResult Register(RegisterModel model, string returnUrl)
        {
            try
            {
                _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                if (_log.IsInfoEnabled)
                {
                    _log.Info("calling register post method");
                }
                if (_log.IsInfoEnabled)
                {
                    _log.Info(ModelState.IsValid);
                }
                if (ModelState.IsValid)
                {
                    // Attempt to register the user
                    if (_log.IsInfoEnabled)
                    {
                        _log.Info("calling CreateUser method of MembershipService");
                    }
                    MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email, model.PlantId);

                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        if (_log.IsInfoEnabled)
                        {
                            _log.Info("User Created from CreateUser method of MembershipService.");
                        }
                        FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
                        UserServices  oUserServices  = new UserServices();
                        var           item           = oUserServices.UserIdByUserName(model.UserName);
                        string        message        = item.IsAnonymous ? "use the password you registered with" : "use your windows logon password";
                        EmailServices oEmailServices = new EmailServices();
                        oEmailServices.RegisteredUserEmail(model.Email, model.UserName, item.UserId, message);

                        AdminServices oAdminServices = new AdminServices();
                        oAdminServices.InsertDefaultUserNotification(item.UserId);

                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") &&
                            !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return(Redirect(returnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                    else
                    {
                        if (_log.IsInfoEnabled)
                        {
                            _log.Info("Failed to register User from CreateUser method of MembershipService.");
                        }
                        ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                    }
                }
            }
            catch (Exception ex)
            {
                if (_log.IsErrorEnabled)
                {
                    _log.Error(ex.Message, ex);
                }
                throw ex;
            }
            // If we got this far, something failed, redisplay form

            ViewBag.PasswordLength = MembershipService.MinPasswordLength;
            model.Plants           = _userServices.GetPlant();
            return(View(model));
        }