Esempio n. 1
0
        /// <summary>
        /// 判断当前微信账号是否已绑定,若绑定则生成currentUser
        /// </summary>
        /// <param name="openId">openId</param>
        /// <returns></returns>
        private bool CreateCurrentUser(string openId)
        {
            using (var unitOfWork = Bootstrapper.Instance.UnityContainer.Resolve<IUnitOfWork>("ReadUnitOfWork"))
            {
                var queryManage = new QueryManage(unitOfWork);
                var commonQuery = new CommonQuery(queryManage);

                var wechartAccount = commonQuery.GetWechartAccount(openId);

                if (wechartAccount == null)
                {
                    return false;
                }

                var user = commonQuery.GetUser(wechartAccount.UserId);

                if (user != null)
                {
                    CommonHelper.InitializationCurrentUser(user, OAuthSource.Wechart);
                    return true;
                }

                return false;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 根据githubUser.id获取userId,若首次访问,则新建OAuthAccount记录和User记录
        /// </summary>
        /// <param name="githubUser"></param>
        /// <returns></returns>
        private User GetOAuthUser(GithubUserInfoResult githubUser)
        {
            using (UnitOfWork)
            {
                User user = null;
                var queryManage = new QueryManage(UnitOfWork);
                var commonQuery = new CommonQuery(queryManage);
                var userRepo = new UserRepository(UnitOfWork);
                var oauthAccountRepo = new BaseRepository<OAuthAccount>(UnitOfWork);

                const string query = @"SELECT *
                                        FROM oauthaccount
                                        WHERE OAuthCode = @OAuthCode
	                                        AND Source = @Source";
                var oauthAccount = queryManage.GetList<OAuthAccount>(query, new { OAuthCode = githubUser.id, Source = OAuthSource.Github })
                    .FirstOrDefault();

                if (oauthAccount == null)
                {
                    user = new User
                    {
                        UserName = githubUser.login,
                        Enable = true,
                        CreatedTime = DateTime.Now
                    };
                    user.Id = userRepo.CreateWithIdentity(user);

                    oauthAccountRepo.Create(new OAuthAccount
                    {
                        OAuthCode = githubUser.id.ToString(CultureInfo.InvariantCulture),
                        Source = OAuthSource.Github,
                        UserId = user.Id,
                        CreatedTime = DateTime.Now
                    });
                }
                else
                {
                    user = commonQuery.GetUser(oauthAccount.UserId);
                }

                return user;
            }
        }