Exemplo n.º 1
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="email">Email</param>
        /// <returns></returns>
        public async Task<UserAccount> CreateAsync(string userName, string password, string email)
        {
            if(!CommonHelper.IsValidEmail(email))
            {
                throw new DisplayableException("邮箱地址不正确");
            }
            if(await GetByUserNameAsync(userName) != null)
            {
                throw new DisplayableException("该用户名已被注册");
            }
            if(await GetByEmailAsync(email) != null)
            {
                throw new DisplayableException("该邮箱地址已被注册");
            }

            string saltKey = KoalaBlogSecurityManager.CreateSaltKey(5);

            UserAccount ua = new UserAccount()
            {
                LastLogon = DateTime.Now,
                UserName = userName,
                Password = KoalaBlogSecurityManager.CreatePasswordHash(password, saltKey),
                PasswordSalt = saltKey,
                Email = email,
                EmailConfirmed = false,
                Status = UserAccount.STATUS_ACTIVE,
                IsOnline = false
            };
            Add(ua);

            return await SaveChangesAsync() > 0 ? ua : null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建Person并且建立UserAccountXPerson的关系
        /// </summary>
        /// <param name="ua">UserAccount</param>
        /// <returns></returns>
        public async Task<Person> CreatePersonAsync(UserAccount ua)
        {
            AssertUtil.IsNotNull(ua, "UserAccount can't be null");

            UserAccountHandler uaHandler = new UserAccountHandler(_dbContext);
            UserAccountXPersonHandler uaxpHandler = new UserAccountXPersonHandler(_dbContext);

            AssertUtil.IsNotNull(await uaHandler.GetByIdAsync(ua.ID), "This user account doesn't exist");

            //1. Check whether the existing relationships.
            AssertUtil.IsTrue(await uaxpHandler.AnyAsync(x => x.UserAccountID == ua.ID), "Existing relationships");

            using(var dbTransaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    //2. Setup the basic profile.
                    Person per = new Person();
                    per.NickName = ua.UserName;
                    per.RealNameAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                    per.SexualTrendAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                    per.MaritalStatusAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                    per.QQAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                    per.DOBAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                    per.BloodTypeAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                    per.HomePageAccessLevel = PersonInfoAccessInfo.MyselfOnly;
                    per.AllowablePersonForComment = AllowablePersonForComment.All;
                    per.AllowCommentAttachContent = true;
                    Add(per);
                    await SaveChangesAsync();

                    UserAccountXPerson uaxp = new UserAccountXPerson();
                    uaxp.UserAccountID = ua.ID;
                    uaxp.PersonID = per.ID;
                    uaxpHandler.Add(uaxp);
                    await SaveChangesAsync();

                    dbTransaction.Commit();

                    return per;
                }
                catch (Exception)
                {
                    dbTransaction.Rollback();
                    throw;
                }
            }
        }
Exemplo n.º 3
0
        public void TestFixtureSetUp()
        {
            TestUtil.CleanUpData();

            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                UserAccountHandler uaHandler = new UserAccountHandler(dbContext);

                testUA1 = new UserAccount();
                testUA1.UserName = "******";
                testUA1.PasswordSalt = "testSalt1";
                testUA1.Password = "******";
                testUA1.Email = "*****@*****.**";
                testUA1.LastLogon = DateTime.Now;
                testUA1.EmailConfirmed = true;
                testUA1.Status = UserAccount.STATUS_ACTIVE;
                uaHandler.Add(testUA1);
                uaHandler.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public async Task<bool> ModifyAsync(UserAccount ua)
        {
            MarkAsModified(ua);

            await SaveChangesAsync();

            return true;
        }
Exemplo n.º 5
0
        public async Task Test_01_CreatePersonAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);
                UserAccountXPersonHandler uaxpHandler = new UserAccountXPersonHandler(dbContext);

                //1. Test normal create person
                Person p = await perHandler.CreatePersonAsync(testUA1);

                Assert.IsNotNull(p);
                Assert.AreEqual(p.NickName, "testUserAccount");
                Assert.AreEqual(p.Gender, null);
                Assert.AreEqual(p.RealNameAccessLevel, PersonInfoAccessInfo.MyselfOnly);
                Assert.AreEqual(p.SexualTrendAccessLevel, PersonInfoAccessInfo.MyselfOnly);
                Assert.AreEqual(p.DOBAccessLevel, PersonInfoAccessInfo.MyselfOnly);

                //2. Get the UserAccountXPerson and test
                UserAccountXPerson uaxp = await uaxpHandler.LoadByUserAccountIDAsync(testUA1.ID);
                Assert.IsNotNull(uaxp);
                Assert.AreEqual(uaxp.PersonID, p.ID);
                Assert.AreEqual(uaxp.UserAccountID, testUA1.ID);

                //3. Give the null parameter and check it.
                bool isChecked = false;
                try
                {
                    Person per = await perHandler.CreatePersonAsync(null);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "UserAccount can't be null");
                }
                Assert.IsTrue(isChecked);

                //4. Give the error user account and check it.
                isChecked = false;
                try
                {
                    UserAccount ua = new UserAccount() { ID = 99999 };
                    Person per = await perHandler.CreatePersonAsync(ua);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "This user account doesn't exist");
                }
                Assert.IsTrue(isChecked);

                //5. Give the same user account and check it.
                isChecked = false;
                try
                {
                    Person per = await perHandler.CreatePersonAsync(testUA1);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(AssertException));
                    Assert.AreEqual(ex.Message, "Existing relationships");
                }
                Assert.IsTrue(isChecked);
            }
        }