Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
0
        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;
        }
Ejemplo n.º 5
0
        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;
        }
Ejemplo n.º 6
0
 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;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Generic single entity delete
 /// </summary>
 /// <param name="user"></param>
 public void Delete(User user)
 {
     this._context.User.Remove(user);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Add a new user
 /// </summary>
 /// <param name="newUser"></param>
 public User Add(User newUser)
 {
     return this._context.User.Add(newUser);
 }
Ejemplo n.º 9
0
 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;
 }
Ejemplo n.º 10
0
        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();
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Delete a member
 /// </summary>
 /// <param name="user"></param>
 public bool Delete(User user)
 {
     return false;
 }
Ejemplo n.º 13
0
 public Role UsersRole(User user)
 {
     return user == null ? this._roleService.GetRole(AppConstants.GuestRoleName) : user.Role;
 }
Ejemplo n.º 14
0
 /// <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;
 }
Ejemplo n.º 15
0
        /// <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);
        }
Ejemplo n.º 16
0
 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;
 }
Ejemplo n.º 17
0
 /// <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;
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Save user (does NOT update password data)
 /// </summary>
 /// <param name="user"></param>
 public void ProfileUpdated(User user)
 {
 }
Ejemplo n.º 19
0
        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);
        }
Ejemplo n.º 20
0
        /// <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;
        }