Esempio n. 1
0
        /// <summary>
        /// 获取完整的用户实体
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="userName">用户名</param>
        public static User GetFullUser(this IUserService userService, string userName)
        {
            IUserRepository userRepository = userService.GetUserRepository();
            long            userId         = UserIdToUserNameDictionary.GetUserId(userName);

            return(userRepository.GetUser(userId));
        }
Esempio n. 2
0
        /// <summary>
        /// 验证提供的用户名和密码是否匹配
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>返回<see cref="UserLoginStatus"/></returns>
        public UserLoginStatus ValidateUser(string username, string password)
        {
            long userId = UserIdToUserNameDictionary.GetUserId(username);

            User user = userRepository.Get(userId);

            if (user == null)
            {
                return(UserLoginStatus.InvalidCredentials);
            }

            if (!UserPasswordHelper.CheckPassword(password, user.Password, (UserPasswordFormat)user.PasswordFormat))
            {
                return(UserLoginStatus.InvalidCredentials);
            }

            if (!user.IsActivated)
            {
                return(UserLoginStatus.NotActivated);
            }
            if (user.IsBanned)
            {
                if (user.BanDeadline >= DateTime.UtcNow)
                {
                    return(UserLoginStatus.Banned);
                }
                else
                {
                    user.IsBanned    = false;
                    user.BanDeadline = DateTime.UtcNow;
                    userRepository.Update(user);
                }
            }
            return(UserLoginStatus.Success);
        }
Esempio n. 3
0
        /// <summary>
        /// 用户解除管制后增加邀请人积分
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        void FreeModeratedUser_After(User sender, CommonEventArgs eventArgs)
        {
            if (sender == null || string.IsNullOrEmpty(eventArgs.EventOperationType))
            {
                return;
            }
            if (eventArgs.EventOperationType == EventOperationType.Instance().CancelModerateUser() || eventArgs.EventOperationType == EventOperationType.Instance().AutoNoModeratedUser())
            {
                PointService pointService = new PointService();
                string       pointItemKey = string.Empty;
                pointItemKey = PointItemKeys.Instance().FreeModeratedUser();

                if (sender != null)
                {
                    InviteFriendRecord invitingUser = inviteFriendService.GetInvitingUserId(sender.UserId);
                    if (invitingUser != null)
                    {
                        if (!invitingUser.InvitingUserHasBeingRewarded)
                        {
                            string userName    = UserIdToUserNameDictionary.GetUserName(invitingUser.UserId);
                            string invitedName = UserIdToUserNameDictionary.GetUserName(sender.UserId);
                            string description = string.Format(ResourceAccessor.GetString("PointRecord_Pattern_FreeModeratedUser"), userName, invitedName);
                            pointService.GenerateByRole(invitingUser.UserId, pointItemKey, description);
                            inviteFriendService.RewardingUser(invitingUser.UserId);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 转换成数据库存储的评论对象
        /// </summary>
        /// <returns></returns>
        public Comment AsComment()
        {
            bool notLogin = UserContext.CurrentUser == null;

            Comment comment = Comment.New();

            comment.ParentId          = this.ParentId;
            comment.CommentedObjectId = this.CommentedObjectId;
            comment.OwnerId           = this.OwnerId;
            comment.TenantTypeId      = this.TenantTypeId;
            comment.Subject           = this.Subject ?? string.Empty;
            //this.Body = WebUtility.HtmlEncode(this.Body);
            //comment.Body = new EmotionService().EmoticonTransforms(this.Body);
            comment.Body              = this.Body;
            comment.IsPrivate         = this.IsPrivate;
            comment.ChildCount        = 0;
            comment.IsAnonymous       = notLogin;
            comment.ToUserDisplayName = this.ToUserId <= 0 ? string.Empty : UserIdToUserNameDictionary.GetUserName(this.ToUserId);
            comment.ToUserId          = this.ToUserId;
            comment.UserId            = notLogin ? 0 : UserContext.CurrentUser.UserId;
            comment.AuditStatus       = AuditStatus.Success;
            comment.Author            = notLogin ? this.Author : UserContext.CurrentUser.DisplayName;
            comment.Contact           = notLogin ? this.Contact : string.Empty;
            return(comment);
        }
Esempio n. 5
0
        /// <summary>
        /// 是否开始使用验证码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="scenarios">验证码使用场景</param>
        /// <returns></returns>
        public static bool UseCaptcha(VerifyScenarios scenarios = VerifyScenarios.Post, bool isLimitCount = false)
        {
            CaptchaSettings verificationCodeSettings = CaptchaSettings.Instance();

            if (!verificationCodeSettings.EnableCaptcha)
            {
                return(false);
            }
            IUser currentUser = UserContext.CurrentUser;

            if (scenarios == VerifyScenarios.Register || currentUser == null && scenarios == VerifyScenarios.Post)
            {
                return(true);
            }
            //后台登陆
            if (scenarios == VerifyScenarios.Login && currentUser != null)
            {
                return(true);
            }

            if (currentUser == null && scenarios == VerifyScenarios.Post && verificationCodeSettings.EnableAnonymousCaptcha)
            {
                return(true);
            }
            string userName = GetUserName();

            if (scenarios == VerifyScenarios.Login && UserIdToUserNameDictionary.GetUserId(userName) == 0)
            {
                return(false);
            }

            string cacheKey      = GetCacheKey_LimitTryCount(userName, scenarios);
            int?   limitTryCount = cacheService.Get(cacheKey) as int?;

            if (limitTryCount.HasValue &&
                ((scenarios == VerifyScenarios.Login && limitTryCount >= verificationCodeSettings.CaptchaLoginCount) ||
                 (scenarios == VerifyScenarios.Post && limitTryCount >= verificationCodeSettings.CaptchaPostCount)))
            {
                return(true);
            }

            if (isLimitCount)
            {
                if (limitTryCount.HasValue)
                {
                    limitTryCount++;
                }
                else
                {
                    limitTryCount = 1;
                }
                cacheService.Set(cacheKey, limitTryCount, CachingExpirationType.SingleObject);
            }

            return(false);
        }
 /// <summary>
 /// 创建邀请记录之后的方法
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="eventArgs"></param>
 void CreateInviteFriendRecordEventModule_After(InviteFriendRecord sender, CommonEventArgs eventArgs)
 {
     if (eventArgs.EventOperationType == EventOperationType.Instance().Create())
     {
         PointService pointService = new PointService();
         string       userName     = UserIdToUserNameDictionary.GetUserName(sender.UserId);
         string       invitedName  = UserIdToUserNameDictionary.GetUserName(sender.InvitedUserId);
         string       description  = string.Format(ResourceAccessor.GetString("PointRecord_Pattern_CreateInviteFriendRecord"), userName, invitedName);
         pointService.GenerateByRole(sender.UserId, PointItemKeys.Instance().InviteUserRegister(), description, true);
     }
 }
Esempio n. 7
0
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="takeOverUserName">用于接管删除用户的内容(例如:用户创建的群组)</param>
        /// <param name="takeOverAll">是否接管被删除用户的所有内容</param>
        /// <remarks>接管被删除用户的所有内容</remarks>
        /// <returns></returns>
        public UserDeleteStatus DeleteUser(long userId, string takeOverUserName, bool takeOverAll)
        {
            User user = userRepository.Get(userId);

            if (user == null)
            {
                return(UserDeleteStatus.DeletingUserNotFound);
            }

            if (takeOverAll)
            {
                long takeOverUserId = userRepository.GetUserIdByUserName(takeOverUserName);
                User takeOverUser   = userRepository.Get(takeOverUserId);
                if (takeOverUser == null)
                {
                    return(UserDeleteStatus.InvalidTakeOverUsername);
                }
            }

            if (!user.IsModerated && !user.IsForceModerated)
            {
                // 邀请用户被删除时扣除邀请人积分
                PointService pointService = new PointService();
                string       pointItemKey = string.Empty;
                pointItemKey = PointItemKeys.Instance().DeleteInvitedUser();

                InviteFriendRecord invitingUser = new InviteFriendService().GetInvitingUserId(user.UserId);
                if (invitingUser != null)
                {
                    string userName    = UserIdToUserNameDictionary.GetUserName(invitingUser.UserId);
                    string invitedName = UserIdToUserNameDictionary.GetUserName(user.UserId);
                    string description = string.Format(ResourceAccessor.GetString("PointRecord_Pattern_DeleteInvitedUser"), userName, invitedName);
                    pointService.GenerateByRole(invitingUser.UserId, pointItemKey, description);
                }
            }
            EventBus <User, DeleteUserEventArgs> .Instance().OnBefore(user, new DeleteUserEventArgs(takeOverUserName, takeOverAll));

            int affectCount = userRepository.Delete(user);

            if (affectCount > 0)
            {
                UserIdToUserNameDictionary.RemoveUserId(userId);
                UserIdToUserNameDictionary.RemoveUserName(user.UserName);
                EventBus <User, DeleteUserEventArgs> .Instance().OnAfter(user, new DeleteUserEventArgs(takeOverUserName, takeOverAll));

                return(UserDeleteStatus.Deleted);
            }
            return(UserDeleteStatus.UnknownFailure);
        }
 /// <summary>
 /// 获取UserIdToUserNameAccessor实例
 /// </summary>
 /// <returns></returns>
 private static UserIdToUserNameDictionary Instance()
 {
     if (_defaultInstance == null)
     {
         lock (lockObject)
         {
             if (_defaultInstance == null)
             {
                 _defaultInstance = DIContainer.Resolve <UserIdToUserNameDictionary>();
                 if (_defaultInstance == null)
                 {
                     throw new ExceptionFacade("未在DIContainer注册UserIdToUserNameDictionary的具体实现类");
                 }
             }
         }
     }
     return(_defaultInstance);
 }
Esempio n. 9
0
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userName">用户名</param>
        public IUser GetUser(string userName)
        {
            long userId = UserIdToUserNameDictionary.GetUserId(userName);

            return(GetUser(userId));
        }
 /// <summary>
 /// 获取UserIdToUserNameAccessor实例
 /// </summary>
 /// <returns></returns>
 private static UserIdToUserNameDictionary Instance()
 {
     if (_defaultInstance == null)
     {
         lock (lockObject)
         {
             if (_defaultInstance == null)
             {
                 _defaultInstance = DIContainer.Resolve<UserIdToUserNameDictionary>();
                 if (_defaultInstance == null)
                     throw new ExceptionFacade("未在DIContainer注册UserIdToUserNameDictionary的具体实现类");
             }
         }
     }
     return _defaultInstance;
 }
Esempio n. 11
0
 /// <summary>
 /// 处理地址
 /// </summary>
 /// <param name="userId"></param>
 /// <returns></returns>
 public string GetProcessUrl(long userId)
 {
     return(SiteUrls.FullUrl(SiteUrls.Instance().ListMessageSessions(UserIdToUserNameDictionary.GetUserName(userId), null)));
 }
Esempio n. 12
0
        /// <summary>
        /// 通知处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        void CommentNoticeEventModule_After(Comment sender, AuditEventArgs eventArgs)
        {
            AuditService auditService   = new AuditService();
            bool?        auditDirection = auditService.ResolveAuditDirection(eventArgs.OldAuditStatus, eventArgs.NewAuditStatus);

            if (auditDirection == true)
            {
                var urlGetter       = CommentUrlGetterFactory.Get(sender.TenantTypeId);
                var commentedObject = urlGetter.GetCommentedObject(sender.CommentedObjectId);
                var senderUser      = sender.User();
                if (urlGetter == null || commentedObject == null)
                {
                    return;
                }
                //文章有新评论时,自动通知原作者
                var toUserIds = new List <long>();
                //评论相关的atuserid
                List <long> userids = new AtUserService(TenantTypeIds.Instance().Comment()).GetAtUserIds(sender.Id);

                if (!userids.Contains(commentedObject.UserId))
                {
                    toUserIds.Add(commentedObject.UserId);
                }

                if (sender.ParentId > 0 && !userids.Contains(sender.ToUserId))
                {
                    toUserIds.Add(sender.ToUserId);
                }

                foreach (var toUserId in toUserIds)
                {
                    //通知的对象排除掉自己
                    if (toUserId == sender.UserId)
                    {
                        continue;
                    }


                    Notice notice = Notice.New();
                    notice.UserId          = toUserId;
                    notice.ApplicationId   = 0;
                    notice.TypeId          = NoticeTypeIds.Instance().Reply();
                    notice.LeadingActor    = senderUser != null ? senderUser.DisplayName : "匿名用户";
                    notice.LeadingActorUrl = SiteUrls.FullUrl(SiteUrls.Instance().SpaceHome(UserIdToUserNameDictionary.GetUserName(sender.UserId)));

                    notice.RelativeObjectName = StringUtility.Trim(commentedObject.Name, 60);
                    notice.RelativeObjectId   = sender.Id;
                    notice.RelativeObjectUrl  = SiteUrls.FullUrl(urlGetter.GetCommentDetailUrl(sender.CommentedObjectId, sender.Id, commentedObject.UserId)) ?? string.Empty;
                    notice.TemplateName       = sender.ParentId > 0 ? NoticeTemplateNames.Instance().NewReply() : NoticeTemplateNames.Instance().NewComment();
                    new NoticeService().Create(notice);
                }
            }
        }
Esempio n. 13
0
 /// <summary>
 /// 处理地址
 /// </summary>
 /// <param name="userId"></param>
 /// <returns></returns>
 public string GetProcessUrl(long userId)
 {
     return(SiteUrls.FullUrl(SiteUrls.Instance().ListNotices(UserIdToUserNameDictionary.GetUserName(userId), NoticeStatus.Unhandled, null)));
 }