Пример #1
0
        /// <summary>
        /// 从给定的 Session 中抽取当前会话用户权限集。
        /// </summary>
        /// <param name="session">当前会话上下文。</param>
        /// <returns>存在于当前会话中的用户权限集。</returns>
        public static UserGroup GetUserGroup(HttpSessionStateBase session)
        {
            if (session == null)
            {
                return(UserGroup.Guests);
            }

            // 从数据库中查询权限集。
            string username = GetUsername(session);

            if (username == null)
            {
                return(UserGroup.Guests);
            }

            UserHandle handle = UserManager.Default.QueryUserByName(username);

            if (handle == null)
            {
                return(UserGroup.Guests);
            }

            using (UserDataProvider userData = UserDataProvider.Create(handle, true))
            {
                return(userData.UserGroup);
            }
        }
Пример #2
0
        private bool DoRegister(UserRegisterModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (UserManager.Default.IsUserExist(model.Username))
            {
                model.UsernameErrorMessage = "Username already exist.";
                return(false);
            }

            UserHandle handle = UserManager.Default.CreateUser(model.Username);

            UserAuthorization.UpdatePassword(model.Username, model.Password);

            using (UserDataProvider data = UserDataProvider.Create(handle, false))
            {
                data.Sex       = SexConvert.ConvertFromString(model.Sex);
                data.UserGroup = UsergroupConvert.ConvertFromString(model.UserGroupName);
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// 更新用户密码信息。
        /// </summary>
        /// <param name="username">用户名。</param>
        /// <param name="password">用户密码。</param>
        /// <exception cref="ArgumentNullException"/>
        public static void UpdatePassword(string username, string password)
        {
            if (username == null)
            {
                throw new ArgumentNullException(nameof(username));
            }
            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            UserHandle handle = UserManager.Default.QueryUserByName(username);

            if (handle == null)
            {
                // 数据库中没有对应用户的实体。
                return;
            }

            byte[] hash = GetPasswordHash(password);
            using (UserDataProvider data = UserDataProvider.Create(handle, false))
            {
                // 更新密码哈希值。
                data.PasswordHash = hash;
            }
        }
Пример #4
0
        /// <summary>
        /// 从用户句柄创建 UserProfileModel 模型对象。
        /// </summary>
        /// <param name="handle">用户句柄。</param>
        /// <returns>UserProfileModel 模型对象。</returns>
        /// <exception cref="ArgumentNullException"/>
        public static UserProfileModel FromUserHandle(UserHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            UserProfileModel model = new UserProfileModel()
            {
                Username = handle.Username
            };

            using (UserDataProvider userData = UserDataProvider.Create(handle, true))
            {
                model.Organization = userData.Organization;
                model.UserGroup    = userData.UserGroup;
                model.Sex          = userData.Sex;

                // 加载用户队伍信息。
                foreach (TeamHandle team in userData.GetTeams())
                {
                    model.Teams.Add(TeamBriefModel.FromTeamHandle(team));
                }

                // TODO: 完成用户提交统计模块后,在这里添加代码将用户提交统计信息复制入模型中。
            }

            return(model);
        }
Пример #5
0
        public ActionResult UserTeams()
        {
            string username = Request.QueryString["username"];

            if (string.IsNullOrEmpty(username))
            {
                return(QueryResult(1, "Username is required."));
            }

            if (!UserManager.Default.IsUserExist(username))
            {
                return(QueryResult(2, "Username is not exist."));
            }

            UserHandle user = new UserHandle(username);

            List <TeamDetailModel> teams = new List <TeamDetailModel>();

            using (UserDataProvider userData = UserDataProvider.Create(user, true))
            {
                foreach (TeamHandle team in userData.GetTeams())
                {
                    teams.Add(TeamDetailModel.FromTeamHandle(team));
                }
            }

            return(QueryResult(teams));
        }
Пример #6
0
        /// <summary>
        /// 提供快捷方法获取给定用户的操作权限信息。
        /// </summary>
        /// <param name="handle">用户句柄。</param>
        /// <returns>给定用户的操作权限信息。</returns>
        /// <exception cref="ArgumentNullException"/>
        public static UserGroup GetUserGroup(UserHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            using (UserDataProvider data = UserDataProvider.Create(handle, true))
            {
                return(data.UserGroup);
            }
        }
Пример #7
0
        public ActionResult ChangeUsergroup(FormCollection form)
        {
            // 检查查询参数。
            if (string.IsNullOrEmpty(Request.QueryString["username"]) ||
                string.IsNullOrEmpty(form["usergroup"]))
            {
                return(Redirect("~/Profile"));
            }

            string     username = Request.QueryString["username"];
            UserHandle handle   = UserManager.Default.QueryUserByName(username);

            if (handle == null)
            {
                // 指定的用户不存在。
                return(Redirect("~/Profile"));
            }

            // 检查用户操作权限。
            if (!UserSession.IsAuthorized(Session) ||
                !UserAuthorization.CheckAccessRights(UserGroup.Administrators, UserSession.GetUserGroup(Session)) ||
                string.Compare(username, UserSession.GetUsername(Session), false) == 0)
            {
                return(Redirect("~/Error/AccessDenied"));
            }

            UserGroup targetGroup;

            try
            {
                targetGroup = UsergroupConvert.ConvertFromString(form["usergroup"]);
            }
            catch (ArgumentException)
            {
                return(Redirect("~/Profile"));
            }

            // 执行操作。
            using (UserDataProvider data = UserDataProvider.Create(handle, false))
            {
                data.UserGroup = targetGroup;
            }

            return(Content(string.Empty));
        }
Пример #8
0
        /// <summary>
        /// 检查用户登录验证信息是否正确。
        /// </summary>
        /// <param name="username">用户名。</param>
        /// <param name="password">密码。</param>
        /// <returns>一个值,该值指示用户登录验证信息是否正确。</returns>
        public static bool CheckAuthorization(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return(false);
            }

            UserHandle handle = UserManager.Default.QueryUserByName(username);

            if (handle == null)
            {
                // 用户数据库中没有对应的信息。
                return(false);
            }

            // 计算传入的密码的哈希值。
            byte[] pwdHash = GetPasswordHash(password);

            using (UserDataProvider userData = UserDataProvider.Create(handle, true))
            {
                // 比较密码哈希值是否相同。
                return(Buffer.IsByteArraysEqual(pwdHash, userData.PasswordHash));
            }
        }