A User entity
Inheritance: UserProfile, IMembershipUser
示例#1
0
        public ActionResult CreateUserForm(CreateNewUserModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // Create user entity
            var admin = new User()
            {
                Name = model.Name,
                Username = model.Username,
                Password = model.Password,
                Email = model.Email,
                StartContentHiveId = FixedHiveIds.ContentVirtualRoot,
                StartMediaHiveId = FixedHiveIds.MediaVirtualRoot,
                Applications = new List<string>(new[] { "content", "media", "settings", "developer", "users", "members" }),
                IsApproved = true,
                SessionTimeout = 60
            };

            using (var uow = _requestContext.Application.Hive.OpenWriter<IContentStore>())
            {
                // Find admin usergroup
                var adminUserGroup = uow.Repositories.GetChildren<UserGroup>(
                                    FixedRelationTypes.DefaultRelationType, Framework.Security.Model.FixedHiveIds.UserGroupVirtualRoot)
                                    .Where(y => y.Name == "Administrator")
                                    .FirstOrDefault();

                // Add user to admin role
                if (adminUserGroup != null)
                {
                    admin.Groups = new[] { adminUserGroup.Id };
                }

                // Save user
                MembershipCreateStatus status;
                _requestContext.Application.Security.Users.Create(admin, out status);

                if (status != MembershipCreateStatus.Success)
                {
                    LogHelper.Error<InstallController>(string.Format("Error creating user {0}", admin.Username),
                                                       new ApplicationException(status.ToString()));

                    ModelState.AddModelError("", string.Format("An error occured while creating the user {0}: {1}", admin.Username, status.ToString()));

                    return View(model);
                }
            }

            //TODO: Subscribe user to newsletter
            if(model.SignUpForNewsletter)
            {
                try
                {
                    new WebClient()
                        .UploadValues("http://rebelcms.com/base/Ecom/SubmitEmail/installer.aspx", new NameValueCollection()
                    {
                        {
                            "name",
                            model.Name
                        },
                        {
                            "email",
                            model.Email
                        }
                    });
                }
                catch(Exception ex)
                {
                    LogHelper.TraceIfEnabled<InstallController>("Unable to subscribe user to newsletter: {0}", () => ex.Message);
                }
            }

            return RedirectToAction("StarterKit");
        }
        public void Create_User_With_All_Ids_Asssigned_To_All_Objects()
        {
            var user = new User();

            // Act
            using (var writer = ProviderSetup.UnitFactory.Create())
            {
                writer.EntityRepository.AddOrUpdate(new SystemRoot());
                writer.EntityRepository.AddOrUpdate(FixedEntities.UserVirtualRoot);
                writer.EntityRepository.AddOrUpdate(user);
                writer.Complete();
            }

            foreach(var a in user.Attributes)
            {
                Assert.IsFalse(a.Id.IsNullValueOrEmpty());
            }
            foreach(var g in user.AttributeGroups)
            {
                Assert.IsFalse(g.Id.IsNullValueOrEmpty());
            }
            Assert.IsFalse(user.EntitySchema.Id.IsNullValueOrEmpty());
            foreach(var d in user.EntitySchema.AttributeDefinitions)
            {
                Assert.IsFalse(d.Id.IsNullValueOrEmpty());
            }
            foreach(var g in user.EntitySchema.AttributeGroups)
            {
                Assert.IsFalse(g.Id.IsNullValueOrEmpty());
            }
        }