コード例 #1
0
ファイル: PostService.cs プロジェクト: h2h/CandyBBS
        public Post AddNewPost(string postContent, Topic topic, User user, out PermissionSet permissions)
        {
            permissions = _roleService.GetPermissions(topic.Category, UsersRole(user));

            if (permissions[AppConstants.PermissionDenyAccess].IsTicked || permissions[AppConstants.PermissionReadOnly].IsTicked)
            {
                throw new ApplicationException("");
            }

            var comment = new Post
            {
                PostContent = postContent,
                User = user,
                Topic = topic,
                IpAddress = StringUtils.GetUsersIpAddress(),
                PostType = PostType.comment.ToString(),
                DateCreated = DateTime.UtcNow,
                DateEdited = DateTime.UtcNow
            };

            comment = SanitizePost(comment);

            Add(comment);

            return comment;
        }
コード例 #2
0
ファイル: HtmlHelperExtensions.cs プロジェクト: h2h/CandyBBS
        public static string GetAvatar(this HtmlHelper htmlHelper, User user, int size = 48)
        {
            if (!user.Avatar.IsNullEmpty())
            {
                return user.Avatar;
            }

            if (!Settings(htmlHelper, AppConstants.UseAvatarCache).Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                var avatar = StringUtils.GetGravatarImage(user.Email, size);
                if (!avatar.IsNullEmpty())
                {
                    return avatar;
                }
                return StringUtils.GetDefaultAvatar();
            }

            var avatarPath = System.IO.Path.Combine(AppConstants.AvatarCachePath, StringUtils.md5HashString(user.Email + size.ToString()) + ".jpg");

            if (File.Exists(HttpContext.Current.Server.MapPath(avatarPath)))
            {
                return avatarPath;
            }

            if (AppHelpers.DownloadAvatar(StringUtils.GetGravatarImage(user.Email, size), avatarPath))
            {
                return avatarPath;
            }
            return StringUtils.GetGravatarImage(user.Email, size);
        }
コード例 #3
0
ファイル: CommentController.cs プロジェクト: h2h/CandyBBS
 public CommentController(ILoggingService loggingService, IUnitOfWorkManager unitOfWorkManager, 
     ICategoryService categoryService,
     IUserService userService,
     IRoleService roleService,
     ISettingsService settingsService,
     ITopicService topicService,
     IPostService postService)
     : base(loggingService, unitOfWorkManager,userService,roleService,settingsService)
 {
     this._topicService = topicService;
     this._categoryService = categoryService;
     this._postService = postService;
     LoggedUser = UserIsAuthenticated ? UserService.GetUser(Username) : null;
 }
コード例 #4
0
ファイル: CategoryController.cs プロジェクト: h2h/CandyBBS
        public CategoryController(ILoggingService loggingService, IUnitOfWorkManager unitOfWorkManager,
            IUserService userService,
            IRoleService roleService,
            ICategoryService categoryService,
            ITopicService topicService,
            ISettingsService settingsService)
            : base(loggingService, unitOfWorkManager, userService, roleService, settingsService)
        {
            this._categoryService = categoryService;
            this._topicService = topicService;

            LoggedOnUser = UserIsAuthenticated ? UserService.GetUser(Username) : null;
            UsersRole = LoggedOnUser == null ? RoleService.GetRole(AppConstants.GuestRoleName) : LoggedOnUser.Role;
        }
コード例 #5
0
ファイル: WidgetController.cs プロジェクト: h2h/CandyBBS
        public WidgetController(ILoggingService loggingService, IUnitOfWorkManager unitOfWorkManager,
            IUserService userService,
            IRoleService roleService,
            ISettingsService settingsService,
            IPostService postService,
            ITopicTagService topicTagService,
            ITopicService topicService)
            : base(loggingService, unitOfWorkManager, userService, roleService, settingsService)
        {
            this.LoggedOnUser = UserIsAuthenticated ? UserService.GetUser(Username) : null;
            this.UsersRole = LoggedOnUser == null ? RoleService.GetRole(AppConstants.GuestRoleName) : LoggedOnUser.Role;

            TopicTagService = topicTagService;
            TopicService = topicService;
        }
コード例 #6
0
ファイル: UserMetaService.cs プロジェクト: h2h/CandyBBS
 public User Create(User user)
 {
     if (user != null && user.UserMeta.Any())
     {
         var userMetaList = GetByUser(user.Id);
         var newUserMetaList = new List<UserMeta>();
         foreach (var meta in user.UserMeta)
         {
             if (userMetaList.Any(a => a.MetaKey == meta.MetaKey))
             {
                 var oldMeta = userMetaList.Where(a => a.MetaKey == meta.MetaKey).FirstOrDefault();
                 oldMeta.MetaValue = meta.MetaValue;
                 newUserMetaList.Add(oldMeta);
             }
             else
             {
                 newUserMetaList.Add(_userMetaRepository.Create(meta));
             }
         }
         user.UserMeta = newUserMetaList;
     }
     return user;
 }
コード例 #7
0
ファイル: UserRepository.cs プロジェクト: h2h/CandyBBS
 /// <summary>
 /// Generic single entity delete
 /// </summary>
 /// <param name="user"></param>
 public void Delete(User user)
 {
     this._context.User.Remove(user);
 }
コード例 #8
0
ファイル: UserRepository.cs プロジェクト: h2h/CandyBBS
 /// <summary>
 /// Add a new user
 /// </summary>
 /// <param name="newUser"></param>
 public User Add(User newUser)
 {
     return this._context.User.Add(newUser);
 }
コード例 #9
0
ファイル: UserRepository.cs プロジェクト: h2h/CandyBBS
 public void Update(User item)
 {
     // Check there's not an object with same identifier already in context
     if (this._context.User.Local.Select(x => x.Id == item.Id).Any())
     {
         throw new ApplicationException("Object already exists in context - you do not need to call Update. Save occurs on Commit");
     }
     this._context.Entry(item).State = EntityState.Modified;
 }
コード例 #10
0
ファイル: UserController.cs プロジェクト: h2h/CandyBBS
        public ActionResult Register(RegisterViewModel model)
        {
            if (model.Password.IsNullEmpty())
            {
                ModelState.AddModelError(AppConstants.MessageViewBagName, LocalizerHelper.Lang("密码不能为空"));
                return View();
            }
            if (model.UserName.IsNullEmpty())
            {
                ModelState.AddModelError(AppConstants.MessageViewBagName, LocalizerHelper.Lang("用户名不能为空"));
                return View();
            }
            if (model.Email.IsNullEmpty())
            {
                ModelState.AddModelError(AppConstants.MessageViewBagName, LocalizerHelper.Lang("邮箱不能为空"));
                return View();
            }
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var user = new User();

                user.Email = model.Email;
                user.Password = model.Password;
                user.UserName = model.UserName;

                user.LastLoginDate = DateTime.UtcNow;
                user.CreateDate = DateTime.UtcNow;
                user.ActivationKey = string.Empty;

                var createStatus = UserService.CreateUser(user);

                if (createStatus != UserStatus.Success)
                {
                    ModelState.AddModelError(AppConstants.MessageViewBagName, UserService.ErrorCodeToString(createStatus));
                }
                else
                {
                    unitOfWork.Commit();
                    return RedirectToAction("Activation", new { email = model.Email });
                }
            }
            return View();
        }
コード例 #11
0
ファイル: UserController.cs プロジェクト: h2h/CandyBBS
        public JsonResult LoginForJson(LoginViewModel model)
        {
            var result = new LoginMessageViewModel();
            result.Result = "false";
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var username = model.UserName;
                var password = model.Password;
                try
                {
                    if (ModelState.IsValid)
                    {
                        var user = new User();
                        var loginAttemptStatus = UserService.ValidateUser(username, password, 3);
                        if (loginAttemptStatus == LoginAttemptStatus.LoginSuccessful)
                        {
                            user = UserService.GetUser(username);
                            if (user.ActivationKey.IsNullEmpty())
                            {
                                FormsAuthentication.SetAuthCookie(username, model.RememberMe);
                                user.LastLoginDate = DateTime.UtcNow;

                                if (Url.IsLocalUrl(model.ReturnUrl) && model.ReturnUrl.Length > 1 && model.ReturnUrl.StartsWith("/") && !model.ReturnUrl.StartsWith("//") && !model.ReturnUrl.StartsWith("/\\"))
                                {
                                    result.Result = "true";
                                    result.Message = LocalizerHelper.Lang("登录成功");
                                    result.ReturnUrl = Url.Absolute(model.ReturnUrl);
                                }
                                result.Result = "true";
                                result.Message = LocalizerHelper.Lang("登录成功");
                                result.ReturnUrl = Url.Absolute("~/");
                            }
                            else
                            {
                                result.Result = "false";
                                result.Message = LocalizerHelper.Lang(string.Format("账号未激活,<a href=\"{0}\">现在激活</a>", Url.Absolute("~/Activation/")));
                            }
                        }
                        else
                        {
                            result.Result = "false";
                            result.Message = LocalizerHelper.Lang("账号或密码错误");
                        }
                    }
                }
                finally
                {
                    try
                    {
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                    }
                }
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
コード例 #12
0
ファイル: UserService.cs プロジェクト: h2h/CandyBBS
 /// <summary>
 /// Delete a member
 /// </summary>
 /// <param name="user"></param>
 public bool Delete(User user)
 {
     return false;
 }
コード例 #13
0
ファイル: PostService.cs プロジェクト: h2h/CandyBBS
 public Role UsersRole(User user)
 {
     return user == null ? this._roleService.GetRole(AppConstants.GuestRoleName) : user.Role;
 }
コード例 #14
0
ファイル: UserService.cs プロジェクト: h2h/CandyBBS
 /// <summary>
 /// Change the user's password
 /// </summary>
 /// <param name="user"> </param>
 /// <param name="oldPassword"></param>
 /// <param name="newPassword"></param>
 /// <returns></returns>
 public bool ChangePassword(User user, string oldPassword, string newPassword)
 {
     return true;
 }
コード例 #15
0
ファイル: UserService.cs プロジェクト: h2h/CandyBBS
        /// <summary>
        /// Save user (does NOT update password data)
        /// </summary>
        /// <param name="user"></param>
        public void Save(User user)
        {
            user = SanitizeUser(user);

            this._userRepository.Update(user);
        }
コード例 #16
0
ファイル: UserService.cs プロジェクト: h2h/CandyBBS
 public User SanitizeUser(User User)
 {
     User.Avatar = StringUtils.SafePlainText(User.Avatar);
     User.Email = StringUtils.SafePlainText(User.Email);
     User.Password = StringUtils.SafePlainText(User.Password);
     User.PasswordAnswer = StringUtils.SafePlainText(User.PasswordAnswer);
     User.PasswordQuestion = StringUtils.SafePlainText(User.PasswordQuestion);
     User.Signature = StringUtils.GetSafeHtml(User.Signature);
     User.UserName = StringUtils.SafePlainText(User.UserName);
     User.Website = StringUtils.SafePlainText(User.Website);
     return User;
 }
コード例 #17
0
ファイル: UserService.cs プロジェクト: h2h/CandyBBS
 /// <summary>
 /// Reset a users password
 /// </summary>
 /// <param name="user"></param>
 /// <param name="newPassword"> </param>
 /// <returns></returns>
 public bool ResetPassword(User user, string newPassword)
 {
     return true;
 }
コード例 #18
0
ファイル: UserService.cs プロジェクト: h2h/CandyBBS
 /// <summary>
 /// Save user (does NOT update password data)
 /// </summary>
 /// <param name="user"></param>
 public void ProfileUpdated(User user)
 {
 }
コード例 #19
0
ファイル: UserController.cs プロジェクト: h2h/CandyBBS
        public ActionResult Login(LoginViewModel model)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var username = model.UserName;
                var password = model.Password;
                try
                {
                    if (ModelState.IsValid)
                    {
                        var user = new User();
                        var loginAttemptStatus = UserService.ValidateUser(username, password, 3);
                        if (loginAttemptStatus == LoginAttemptStatus.LoginSuccessful)
                        {
                            user = UserService.GetUser(username);
                            if (user.ActivationKey.IsNullEmpty())
                            {
                                FormsAuthentication.SetAuthCookie(username, model.RememberMe);
                                user.LastLoginDate = DateTime.UtcNow;

                                if (Url.IsLocalUrl(model.ReturnUrl) && model.ReturnUrl.Length > 1 && model.ReturnUrl.StartsWith("/") && !model.ReturnUrl.StartsWith("//") && !model.ReturnUrl.StartsWith("/\\"))
                                {
                                    return Redirect(model.ReturnUrl);
                                }
                                return RedirectToAction("Index", "Home");
                            }
                        }
                    }
                }
                finally
                {
                    try
                    {
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LoggingService.Error(ex);
                    }
                }
            }
            return View(model);
        }
コード例 #20
0
ファイル: UserService.cs プロジェクト: h2h/CandyBBS
        /// <summary>
        /// Create new user
        /// </summary>
        /// <param name="newUser"></param>
        /// <returns></returns>
        public UserStatus CreateUser(User model)
        {
            model = SanitizeUser(model);

            var status = UserStatus.Success;

            if (string.IsNullOrEmpty(model.UserName))
            {
                status = UserStatus.InvalidUserName;
            }
            if (this._userRepository.GetUser(model.UserName) != null)
            {
                status = UserStatus.DuplicateUserName;
            }
            if (this._userRepository.GetUserByEmail(model.Email) != null)
            {
                status = UserStatus.DuplicateEmail;
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                status = UserStatus.InvalidPassword;
            }
            if (status == UserStatus.Success)
            {
                model.Password = GeneratePasswordHash(model.Password);
                var newMemberStartingRole = int.Parse(this._settingsRepository.Get(AppConstants.NewMemberStartingRole).Value);
                model.Role = this._roleRepository.Get(newMemberStartingRole);
                model.Slug = model.UserName;
                try
                {
                    if (this._settingsRepository.Get(AppConstants.EmailAdminOnNewMemberSignUp).Value.Equals("true"))
                    {
                        model.ActivationKey = GeneratePasswordHash(model.UserName).Substring(8, 8);
                        var result = this._userRepository.Add(model);
                        // 发送邮件给管理员,
                        // 发送激活信息给注册用户
                        var email = new Email
                        {
                            Body = _emailService.ActivationTemplate(model.UserName,model.ActivationKey),
                            EmailFrom = _settingsRepository.Get(AppConstants.NotificationReplyEmail).Value,
                            EmailTo = model.Email,
                            NameTo = model.UserName,
                            Subject = string.Format("{0}{1}",_settingsRepository.Get(AppConstants.SiteName).Value, _localizationService.Get("账号激活邮件"))
                        };
                        _emailService.SendMail(email);
                    }
                    else
                    {
                        this._userRepository.Add(model);
                        model.ActivationKey = string.Empty;
                    }
                }catch(Exception)
                {
                    status = UserStatus.UserRejected;
                }
            }
            return UserStatus.Success;
        }