diff --git a/Niqiu.Core/Helpers/SendMail.cs b/Niqiu.Core/Helpers/SendMail.cs index 7e44fc4..7983fcb 100644 --- a/Niqiu.Core/Helpers/SendMail.cs +++ b/Niqiu.Core/Helpers/SendMail.cs @@ -1,192 +1,192 @@ -using System; -using System.IO; -using System.Net.Mail; - -namespace Niqiu.Core.Helpers -{ - public class SendMail - { - - - /// - /// The email gateway - /// - private EmailGateway _emailGateway = new EmailGateway(); - - /// - /// Gets or sets the email gateway. - /// - /// The email gateway. - public EmailGateway EmailGateway - { - get { return _emailGateway; } - set { _emailGateway = value; } - } - - /// - /// Gets or sets the mail host. - /// - /// The mail host. - public string MailHost - { - get { return EmailGateway.MailHost; } - set { EmailGateway.MailHost = value; } - } - - /// - /// Gets or sets the mail port. - /// - /// The mail port. - public int MailPort - { - get { return EmailGateway.MailPort; } - set { EmailGateway.MailPort = value; } - } - - /// - /// Gets or sets the mailhost username. - /// - /// The mailhost username. - public string MailhostUsername - { - get { return EmailGateway.MailHostUsername; } - set { EmailGateway.MailHostUsername = value; } - } - - /// - /// Gets or sets the mailhost password. - /// - /// The mailhost password. - public string MailhostPassword - { - get { return EmailGateway.MailHostPassword; } - set { EmailGateway.MailHostPassword = value; } - } - - /// - /// Gets or sets from address. - /// - /// From address. - public string FromAddress { get; set; } - - /// - /// Gets or sets a value indicating whether [use SSL]. - /// - /// true if [use SSL]; otherwise, false. - public bool UseSSL - { - get { return EmailGateway.UseSSL; } - set { EmailGateway.UseSSL = value; } - } - - /// - /// Gets or sets the reply to address. - /// - /// The reply to address. - public string ReplyToAddress { get; set; } - - private void SendMessage(string from, string to, string replyto, string subject, string message, string workingFolder) - { - try - { - using (var actualMessage = GetMailMessage(from, to, replyto, subject, message, workingFolder, null)) - { - _emailGateway.Send(actualMessage); - } - } - catch (Exception e) - { - throw new Exception("EmailPublisher exception: " + e, e); - } - } - - public bool SendaMail(string toEmails, string emailText, string subject = "日志系統(SI)") - { - - const string systermtxt = "该邮件为系统自动发送,请勿回复"; - bool isSendOk; - try - { - SendMessage(FromAddress, toEmails, "", subject, emailText + systermtxt, ""); - isSendOk = true; - } - catch (Exception e) - { - isSendOk = false; - } - return isSendOk; - } - - public SendMail() - { - MailPort = 25; - UseSSL = false; - - MailhostUsername = "Dawn.wang"; - MailhostPassword = "Delta123456"; - FromAddress = "DAWN.WANG@DELTAWW.COM.CN"; - MailHost = "172.17.161.8"; - - //MailhostPassword = "stone123"; - //FromAddress = "stonzrj@163.com"; - //MailHost = "smtp.163.com"; - //MailhostUsername = "stonzrj"; - } - - public SendMail(string fromemail,string name, string pwd, string host) - { - FromAddress = fromemail; - MailhostUsername = name; - MailhostPassword = pwd; - MailHost = host; - UseSSL = false; - MailPort = 25; - } - - - protected static MailMessage GetMailMessage(string from, string to, string replyto, string subject, - string messageText, string workingFolder, string[] attachments) - { - var mailMessage = new MailMessage(); - mailMessage.To.Add(to); - mailMessage.From = new MailAddress(from); - if (!String.IsNullOrEmpty(replyto)) mailMessage.ReplyTo = new MailAddress(replyto); - mailMessage.Subject = subject; - mailMessage.IsBodyHtml = true; - mailMessage.Body = messageText; - mailMessage.Priority = MailPriority.High; - - - // Add any attachments - if (attachments != null) - { - foreach (var attachment in attachments) - { - var fullPath = attachment; - if (!Path.IsPathRooted(fullPath)) fullPath = Path.Combine(workingFolder, fullPath); - if (File.Exists(fullPath)) - { - var mailAttachment = new Attachment(fullPath); - mailMessage.Attachments.Add(mailAttachment); - } - } - } - - return mailMessage; - } - /// - /// 异步发送邮件 - /// - /// - /// - /// - public void SyncSendMail(string toMails, string content, string subject = "DIAView") - { - Action invokeAction = () => SendaMail(toMails, content, subject); - invokeAction.BeginInvoke(null, invokeAction); - } - - - - } +using System; +using System.IO; +using System.Net.Mail; + +namespace Niqiu.Core.Helpers +{ + public class SendMail + { + + + /// + /// The email gateway + /// + private EmailGateway _emailGateway = new EmailGateway(); + + /// + /// Gets or sets the email gateway. + /// + /// The email gateway. + public EmailGateway EmailGateway + { + get { return _emailGateway; } + set { _emailGateway = value; } + } + + /// + /// Gets or sets the mail host. + /// + /// The mail host. + public string MailHost + { + get { return EmailGateway.MailHost; } + set { EmailGateway.MailHost = value; } + } + + /// + /// Gets or sets the mail port. + /// + /// The mail port. + public int MailPort + { + get { return EmailGateway.MailPort; } + set { EmailGateway.MailPort = value; } + } + + /// + /// Gets or sets the mailhost username. + /// + /// The mailhost username. + public string MailhostUsername + { + get { return EmailGateway.MailHostUsername; } + set { EmailGateway.MailHostUsername = value; } + } + + /// + /// Gets or sets the mailhost password. + /// + /// The mailhost password. + public string MailhostPassword + { + get { return EmailGateway.MailHostPassword; } + set { EmailGateway.MailHostPassword = value; } + } + + /// + /// Gets or sets from address. + /// + /// From address. + public string FromAddress { get; set; } + + /// + /// Gets or sets a value indicating whether [use SSL]. + /// + /// true if [use SSL]; otherwise, false. + public bool UseSSL + { + get { return EmailGateway.UseSSL; } + set { EmailGateway.UseSSL = value; } + } + + /// + /// Gets or sets the reply to address. + /// + /// The reply to address. + public string ReplyToAddress { get; set; } + + private void SendMessage(string from, string to, string replyto, string subject, string message, string workingFolder) + { + try + { + using (var actualMessage = GetMailMessage(from, to, replyto, subject, message, workingFolder, null)) + { + _emailGateway.Send(actualMessage); + } + } + catch (Exception e) + { + throw new Exception("EmailPublisher exception: " + e, e); + } + } + + public bool SendaMail(string toEmails, string emailText, string subject = "日志系統(SI)") + { + + const string systermtxt = "该邮件为系统自动发送,请勿回复"; + bool isSendOk; + try + { + SendMessage(FromAddress, toEmails, "", subject, emailText + systermtxt, ""); + isSendOk = true; + } + catch (Exception e) + { + isSendOk = false; + } + return isSendOk; + } + + public SendMail() + { + MailPort = 25; + UseSSL = false; + + //MailhostUsername = "Dawn.wang"; + //MailhostPassword = "Delta123456"; + //FromAddress = "DAWN.WANG@DELTAWW.COM.CN"; + //MailHost = "172.17.161.8"; + + MailhostPassword = "stone123"; + FromAddress = "stonzrj@163.com"; + MailHost = "smtp.163.com"; + MailhostUsername = "stonzrj"; + } + + public SendMail(string fromemail, string name, string pwd, string host) + { + FromAddress = fromemail; + MailhostUsername = name; + MailhostPassword = pwd; + MailHost = host; + UseSSL = false; + MailPort = 25; + } + + + protected static MailMessage GetMailMessage(string from, string to, string replyto, string subject, + string messageText, string workingFolder, string[] attachments) + { + var mailMessage = new MailMessage(); + mailMessage.To.Add(to); + mailMessage.From = new MailAddress(from); + if (!String.IsNullOrEmpty(replyto)) mailMessage.ReplyTo = new MailAddress(replyto); + mailMessage.Subject = subject; + mailMessage.IsBodyHtml = true; + mailMessage.Body = messageText; + mailMessage.Priority = MailPriority.High; + + + // Add any attachments + if (attachments != null) + { + foreach (var attachment in attachments) + { + var fullPath = attachment; + if (!Path.IsPathRooted(fullPath)) fullPath = Path.Combine(workingFolder, fullPath); + if (File.Exists(fullPath)) + { + var mailAttachment = new Attachment(fullPath); + mailMessage.Attachments.Add(mailAttachment); + } + } + } + + return mailMessage; + } + /// + /// 异步发送邮件 + /// + /// + /// + /// + public void SyncSendMail(string toMails, string content, string subject = "DIAView") + { + Action invokeAction = () => SendaMail(toMails, content, subject); + invokeAction.BeginInvoke(null, invokeAction); + } + + + + } } \ No newline at end of file diff --git a/Niqiu.Core/Services/AccoutService.cs b/Niqiu.Core/Services/AccoutService.cs index 7663a6a..4d0e345 100644 --- a/Niqiu.Core/Services/AccoutService.cs +++ b/Niqiu.Core/Services/AccoutService.cs @@ -1,266 +1,269 @@ -using System; -using Niqiu.Core.Domain; -using Niqiu.Core.Domain.User; -using Niqiu.Core.Helpers; - -namespace Niqiu.Core.Services -{ - public class AccoutService : IAccountService - { - private readonly IUserService _userService; - - public AccoutService(IUserService userService) - { - _userService = userService; - } - - public UserLoginResults ValidateUser(string usernameOrEmail, string password) - { - User user=null; - - - user = _userService.GetUserByUsername(usernameOrEmail); - if (user == null && usernameOrEmail.Contains("@")) - { - user = _userService.GetUserByEmail(usernameOrEmail); - } - - - if (user == null) - return UserLoginResults.UserNotExist; - if (user.Deleted) - return UserLoginResults.Deleted; - if (!user.Active) - return UserLoginResults.NotActive; - //only registered can login - //if (!user.IsRegistered()) - // return UserLoginResults.NotRegistered; - - string pwd = ""; - //这个地方要注意 日后加强的时候要更改。 - switch (user.PasswordFormat) - { - case PasswordFormat.Encrypted: - pwd = Encrypt.EncryptString(password); - break; - case PasswordFormat.Hashed: - pwd = Encrypt.CreatePasswordHash(password, user.PasswordSalt); - break; - default: - pwd = password; - break; - } - bool isValid = Encrypt.GetMd5Code(pwd) == user.Password; - if (!isValid) - return UserLoginResults.WrongPassword; - - //save last login date - user.LastLoginDateUtc = DateTime.UtcNow; - _userService.UpdateUser(user); - return UserLoginResults.Successful; - } - - public void SetEmail(User user, string newEmail) - { - - } - - public void SetUsername(User user, string newUsername) - { - if (user == null) - throw new ArgumentNullException("user"); - newUsername = newUsername.Trim(); - - if (newUsername.Length > 100) - throw new PortalException("用户名太长"); - - var user2 = _userService.GetUserByUsername(newUsername); - if (user2 != null && user.Id != user2.Id) - throw new PortalException("用户名已经存在"); - - user.Username = newUsername; - _userService.UpdateUser(user); - } - - public UserRegistrationResult RegisterUser(UserRegistrationRequest request) - { - if (request == null) - throw new ArgumentNullException("request"); - - if (request.User == null) - throw new ArgumentException("当前用户为空"); - - var result = new UserRegistrationResult(); - - if (request.User.IsRegistered()) - { - result.AddError("当前用户已经注册"); - return result; - } - if (String.IsNullOrEmpty(request.Email)) - { - result.AddError("邮箱不能为空"); - return result; - } - if (!CommonHelper.IsValidEmail(request.Email)) - { - result.AddError("邮件格式错误"); - return result; - } - if (String.IsNullOrWhiteSpace(request.Password)) - { - result.AddError("密码不能为空"); - return result; - } - if (String.IsNullOrWhiteSpace(request.Mobile)) - { - result.AddError("手机号码不能为空"); - return result; - } - if (_userService.GetUserByUsername(request.Username) != null) - { - result.AddError("用户名已经存在"); - return result; - } - - request.User.Username = request.Username; - request.User.Email = request.Email; - request.User.PasswordFormat = request.PasswordFormat; - request.User.Mobile = request.Mobile; - request.User.ImgUrl = "/Content/user_img.jpg"; - switch (request.PasswordFormat) - { - case PasswordFormat.Clear: - { - request.User.Password = request.Password; - } - break; - case PasswordFormat.Encrypted: - { - request.User.Password = Encrypt.GetMd5Code(request.Password); - } - break; - case PasswordFormat.Hashed: - { - string saltKey = Encrypt.CreateSaltKey(5); - request.User.PasswordSalt = saltKey; - request.User.Password = Encrypt.CreatePasswordHash(request.Password, saltKey); - } - break; - default: - break; - } - request.User.Active = request.IsApproved; - - // 添加基本角色。 - //var registeredRole = _userService.GetUserRoleBySystemName(SystemUserRoleNames.Registered); - //if (registeredRole == null) - // throw new PortalException("'Registered' 角色加载失败"); - if (request.User.Id == 0) - { - - _userService.InsertUser(request.User); - request.User = _userService.GetUserByUsername(request.Username); - } - //request.User.UserRoles.Add(registeredRole); - //_userService.UpdateUser(request.User); - - return result; - } - - public PasswordChangeResult ChangePassword(ChangePasswordRequest request) - { - if (request == null) - throw new ArgumentNullException("request"); - - var result = new PasswordChangeResult(); - if (String.IsNullOrWhiteSpace(request.Email)) - { - result.AddError("邮件不能为空"); - return result; - } - if (String.IsNullOrWhiteSpace(request.NewPassword)) - { - result.AddError("密码不能为空"); - return result; - } - - var customer =_userService.GetUserByEmail(request.Email); - if (customer == null) - { - result.AddError("邮件不存在"); - return result; - } - - var requestIsValid = false; - if (request.ValidateRequest) - { - //password - string oldPwd = ""; - switch (customer.PasswordFormat) - { - case PasswordFormat.Encrypted: - oldPwd = Encrypt.GetMd5Code(request.OldPassword); - break; - case PasswordFormat.Hashed: - oldPwd = Encrypt.CreatePasswordHash(request.OldPassword, customer.PasswordSalt); - break; - default: - oldPwd = request.OldPassword; - break; - } - - bool oldPasswordIsValid = oldPwd == customer.Password; - if (!oldPasswordIsValid) - result.AddError("旧密码错误"); - - if (oldPasswordIsValid) - requestIsValid = true; - } - else - requestIsValid = true; - - if (requestIsValid) - { - switch (request.NewPasswordFormat) - { - case PasswordFormat.Clear: - { - customer.Password = request.NewPassword; - } - break; - case PasswordFormat.Encrypted: - { - customer.Password = Encrypt.GetMd5Code(request.NewPassword); - } - break; - case PasswordFormat.Hashed: - { - string saltKey = Encrypt.CreateSaltKey(5); - customer.PasswordSalt = saltKey; - customer.Password = Encrypt.CreatePasswordHash(request.NewPassword, saltKey); - } - break; - default: - break; - } - customer.PasswordFormat = request.NewPasswordFormat; - _userService.UpdateUser(customer); - } - - return result; - } - - public bool ChangePassword(int userid,string password) - { - var rawuser = _userService.GetUserById(userid); - if (rawuser != null) - { - rawuser.Password = Encrypt.EncryptString(password); - _userService.UpdateUser(rawuser); - return true; - } - return false; - } - } -} +using System; +using Niqiu.Core.Domain; +using Niqiu.Core.Domain.User; +using Niqiu.Core.Helpers; + +namespace Niqiu.Core.Services +{ + public class AccoutService : IAccountService + { + private readonly IUserService _userService; + + public AccoutService(IUserService userService) + { + _userService = userService; + } + + public UserLoginResults ValidateUser(string usernameOrEmail, string password) + { + User user = null; + + + user = _userService.GetUserByUsername(usernameOrEmail); + if (user == null && usernameOrEmail.Contains("@")) + { + user = _userService.GetUserByEmail(usernameOrEmail); + } + + + if (user == null) + return UserLoginResults.UserNotExist; + if (user.Deleted) + return UserLoginResults.Deleted; + if (!user.Active) + return UserLoginResults.NotActive; + //only registered can login + //if (!user.IsRegistered()) + // return UserLoginResults.NotRegistered; + + string pwd = ""; + //这个地方要注意 日后加强的时候要更改。 + switch (user.PasswordFormat) + { + case PasswordFormat.Clear: + pwd = Encrypt.GetMd5Code(password); + break; + case PasswordFormat.Encrypted: + pwd = Encrypt.GetMd5Code(password); + break; + case PasswordFormat.Hashed: + pwd = Encrypt.CreatePasswordHash(password, user.PasswordSalt); + break; + default: + pwd = password; + break; + } + bool isValid = pwd == user.Password; + if (!isValid) + return UserLoginResults.WrongPassword; + + //save last login date + user.LastLoginDateUtc = DateTime.UtcNow; + _userService.UpdateUser(user); + return UserLoginResults.Successful; + } + + public void SetEmail(User user, string newEmail) + { + + } + + public void SetUsername(User user, string newUsername) + { + if (user == null) + throw new ArgumentNullException("user"); + newUsername = newUsername.Trim(); + + if (newUsername.Length > 100) + throw new PortalException("用户名太长"); + + var user2 = _userService.GetUserByUsername(newUsername); + if (user2 != null && user.Id != user2.Id) + throw new PortalException("用户名已经存在"); + + user.Username = newUsername; + _userService.UpdateUser(user); + } + + public UserRegistrationResult RegisterUser(UserRegistrationRequest request) + { + if (request == null) + throw new ArgumentNullException("request"); + + if (request.User == null) + throw new ArgumentException("当前用户为空"); + + var result = new UserRegistrationResult(); + + if (request.User.IsRegistered()) + { + result.AddError("当前用户已经注册"); + return result; + } + if (String.IsNullOrEmpty(request.Email)) + { + result.AddError("邮箱不能为空"); + return result; + } + if (!CommonHelper.IsValidEmail(request.Email)) + { + result.AddError("邮件格式错误"); + return result; + } + if (String.IsNullOrWhiteSpace(request.Password)) + { + result.AddError("密码不能为空"); + return result; + } + if (String.IsNullOrWhiteSpace(request.Mobile)) + { + result.AddError("手机号码不能为空"); + return result; + } + if (_userService.GetUserByUsername(request.Username) != null) + { + result.AddError("用户名已经存在"); + return result; + } + + request.User.Username = request.Username; + request.User.Email = request.Email; + request.User.PasswordFormat = request.PasswordFormat; + request.User.Mobile = request.Mobile; + request.User.ImgUrl = "/Content/user_img.jpg"; + switch (request.PasswordFormat) + { + case PasswordFormat.Clear: + { + request.User.Password = request.Password; + } + break; + case PasswordFormat.Encrypted: + { + request.User.Password = Encrypt.GetMd5Code(request.Password); + } + break; + case PasswordFormat.Hashed: + { + string saltKey = Encrypt.CreateSaltKey(5); + request.User.PasswordSalt = saltKey; + request.User.Password = Encrypt.CreatePasswordHash(request.Password, saltKey); + } + break; + default: + break; + } + request.User.Active = request.IsApproved; + + // 添加基本角色。 + //var registeredRole = _userService.GetUserRoleBySystemName(SystemUserRoleNames.Registered); + //if (registeredRole == null) + // throw new PortalException("'Registered' 角色加载失败"); + if (request.User.Id == 0) + { + + _userService.InsertUser(request.User); + request.User = _userService.GetUserByUsername(request.Username); + } + //request.User.UserRoles.Add(registeredRole); + //_userService.UpdateUser(request.User); + + return result; + } + + public PasswordChangeResult ChangePassword(ChangePasswordRequest request) + { + if (request == null) + throw new ArgumentNullException("request"); + + var result = new PasswordChangeResult(); + if (String.IsNullOrWhiteSpace(request.Email)) + { + result.AddError("邮件不能为空"); + return result; + } + if (String.IsNullOrWhiteSpace(request.NewPassword)) + { + result.AddError("密码不能为空"); + return result; + } + + var customer = _userService.GetUserByEmail(request.Email); + if (customer == null) + { + result.AddError("邮件不存在"); + return result; + } + + var requestIsValid = false; + if (request.ValidateRequest) + { + //password + string oldPwd = ""; + switch (customer.PasswordFormat) + { + case PasswordFormat.Encrypted: + oldPwd = Encrypt.GetMd5Code(request.OldPassword); + break; + case PasswordFormat.Hashed: + oldPwd = Encrypt.CreatePasswordHash(request.OldPassword, customer.PasswordSalt); + break; + default: + oldPwd = request.OldPassword; + break; + } + + bool oldPasswordIsValid = oldPwd == customer.Password; + if (!oldPasswordIsValid) + result.AddError("旧密码错误"); + + if (oldPasswordIsValid) + requestIsValid = true; + } + else + requestIsValid = true; + + if (requestIsValid) + { + switch (request.NewPasswordFormat) + { + case PasswordFormat.Clear: + { + customer.Password = request.NewPassword; + } + break; + case PasswordFormat.Encrypted: + { + customer.Password = Encrypt.GetMd5Code(request.NewPassword); + } + break; + case PasswordFormat.Hashed: + { + string saltKey = Encrypt.CreateSaltKey(5); + customer.PasswordSalt = saltKey; + customer.Password = Encrypt.CreatePasswordHash(request.NewPassword, saltKey); + } + break; + default: + break; + } + customer.PasswordFormat = request.NewPasswordFormat; + _userService.UpdateUser(customer); + } + + return result; + } + + public bool ChangePassword(int userid, string password) + { + var rawuser = _userService.GetUserById(userid); + if (rawuser != null) + { + rawuser.Password = Encrypt.EncryptString(password); + _userService.UpdateUser(rawuser); + return true; + } + return false; + } + } +} diff --git a/Niqiu.Core/Services/UserService.cs b/Niqiu.Core/Services/UserService.cs index 565b29a..e8c8c11 100644 --- a/Niqiu.Core/Services/UserService.cs +++ b/Niqiu.Core/Services/UserService.cs @@ -1,240 +1,240 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Niqiu.Core.Domain; -using Niqiu.Core.Domain.Common; -using Niqiu.Core.Domain.User; - -namespace Niqiu.Core.Services -{ - public class UserService:IUserService - { - private readonly IRepository _useRepository; - private readonly IRepository _userRoleRepository; - private readonly ICacheManager _cacheManager ; - - public UserService(IRepository useRepository,IRepository userRoleRepository,ICacheManager cacheManager) - { - _useRepository = useRepository; - _userRoleRepository = userRoleRepository; - _cacheManager = cacheManager; - } - - - public void DeleteUser(User user) - { - if(user==null) throw new ArgumentNullException("user"); - if(user.IsSystemAccount) throw new PortalException(string.Format("系统用户{0}不能删除",user.SystemName)); - - user.Deleted = true; - if (!String.IsNullOrEmpty(user.Email)) - user.Email += "-DELETED"; - if (!String.IsNullOrEmpty(user.Username)) - user.Username += "-DELETED"; - - UpdateUser(user); - } - - public User GetUserById(int userId) - { - if (userId == 0) - return null; - - return _useRepository.GetById(userId); - } - - public void InsertUser(User user) - { - if (user == null) throw new ArgumentNullException("user"); - - - - _useRepository.Insert(user); - - //event notification - //_eventPublisher.EntityInserted(customer); - } - - public void UpdateUser(User user) - { - if (user == null) - throw new ArgumentNullException("user"); - - _useRepository.Update(user); - - //还触发了事件通知! - //_eventPublisher.EntityUpdated(customer); - } - - public User GetUserByEmail(string email) - { - if (string.IsNullOrWhiteSpace(email)) return null; - - var query = from c in _useRepository.Table - orderby c.Id - where c.Email == email - select c; - - var user = query.FirstOrDefault(); - return user; - } - - public IList GetUsersByIds(int[] userIds) - { - if(userIds==null||userIds.Length==0) - return new List(); - - var quey = _useRepository.Table.Where(n => userIds.Contains(n.Id)); - var users = quey.ToList(); - return userIds.Select(id => users.Find(x => x.Id == id)).Where(user => user != null).ToList(); - } - - public User GetUserByGuid(Guid userGuid) - { - return userGuid == Guid.Empty ? null : - _useRepository.Table.FirstOrDefault(n => n.UserGuid == userGuid); - } - - public User GetUserBySystemName(string systemName) - { - return string.IsNullOrWhiteSpace(systemName) ? null : _useRepository.Table.FirstOrDefault(n => n.SystemName == systemName); - } - - public User GetUserByUsername(string username) - { - return string.IsNullOrWhiteSpace(username) ? null : _useRepository.Table.FirstOrDefault(n => n.Username == username); - } - - - public IPagedList GetAllUsers(string email = null, string username = null, int pageIndex = 0, int pageSize = 2147483647) - { - var query = _useRepository.Table.Where(n=>!n.Deleted); - if (!String.IsNullOrWhiteSpace(email)) - query = query.Where(c => c.Email.Contains(email)); - if (!String.IsNullOrWhiteSpace(username)) - query = query.Where(c => c.Email.Contains(username)); - query = query.OrderByDescending(c => c.CreateTime); - var users = new PagedList(query, pageIndex, pageSize); - return users; - } - - - public IPagedList GetOnlineUsers(DateTime lastActivityFromUtc, int[] userRoleIds, int pageIndex, int pageSize) - { - var query = _useRepository.Table; - query = query.Where(c => lastActivityFromUtc <= c.LastActivityDateUtc); - query = query.Where(c => !c.Deleted); - if (userRoleIds != null && userRoleIds.Length > 0) - query = query.Where(c => c.UserRoles.Select(cr => cr.Id).Intersect(userRoleIds).Any()); - - var users = new PagedList(query, pageIndex, pageSize); - return users; - } - - public virtual User InsertGuestUser() - { - var customer = new User - { - UserGuid = Guid.NewGuid(), - Active = true, - LastActivityDateUtc = DateTime.UtcNow, - }; - - //add to 'Guests' role - //var guestRole = GetUserRoleBySystemName(SystemUserRoleNames.Guests); - //if (guestRole == null) - // throw new PortalException("'Guests' role could not be loaded"); - //customer.UserRoles.Add(guestRole); - - // _useRepository.Insert(customer); - - return customer; - } - - - private const string UserrolesBySystemnameKey = "Portal.userrole.systemname-{0}"; - private const string UserrroleAllKey = "Portal.userrrole.all-{0}"; - private const string UserrrolePatternKey = "Portal.userrrole."; - - #region Role - - public virtual UserRole GetUserRoleBySystemName(string systemName) - { - if (String.IsNullOrWhiteSpace(systemName)) - return null; - var key = string.Format(UserrolesBySystemnameKey, systemName); - return _cacheManager.Get(key, () => - { - var query = from cr in _userRoleRepository.Table - orderby cr.Id - where cr.SystemName == systemName - select cr; - var customerRole = query.FirstOrDefault(); - return customerRole; - }); - } - - public void DeleteUserRole(UserRole role) - { - if (role == null) - throw new ArgumentNullException("role"); - - if (role.IsSystemRole) - throw new PortalException("系统用户不能删除"); - - _userRoleRepository.Delete(role); - - // _cacheManager.RemoveByPattern(UserrolesBySystemnameKey); - - // _eventPublisher.EntityDeleted(customerRole); - - } - - public UserRole GetUserRoleById(int userRoleId) - { - if (userRoleId == 0) - return null; - - return _userRoleRepository.GetById(userRoleId); - } - - /// - /// 缓存查询结果! - /// - /// - /// - public IList GetAllUserRoles(bool showHidden = false) - { - var query = from cr in _userRoleRepository.Table - orderby cr.Name - where (showHidden || cr.Active) - select cr; - return query.ToList(); - } - - public void InsertUserRole(UserRole userRole) - { - if (userRole == null) - throw new ArgumentNullException("userRole"); - - _userRoleRepository.Insert(userRole); - - //event notification - //_eventPublisher.EntityInserted(userRole); - } - - public void UpdateUserRole(UserRole userRole) - { - if (userRole == null) - throw new ArgumentNullException("userRole"); - - _userRoleRepository.Update(userRole); - - //event notification - // _eventPublisher.EntityUpdated(customerRole); - } - - - #endregion - } -} +using System; +using System.Collections.Generic; +using System.Linq; +using Niqiu.Core.Domain; +using Niqiu.Core.Domain.Common; +using Niqiu.Core.Domain.User; + +namespace Niqiu.Core.Services +{ + public class UserService : IUserService + { + private readonly IRepository _useRepository; + private readonly IRepository _userRoleRepository; + private readonly ICacheManager _cacheManager; + + public UserService(IRepository useRepository, IRepository userRoleRepository, ICacheManager cacheManager) + { + _useRepository = useRepository; + _userRoleRepository = userRoleRepository; + _cacheManager = cacheManager; + } + + + public void DeleteUser(User user) + { + if (user == null) throw new ArgumentNullException("user"); + if (user.IsSystemAccount) throw new PortalException(string.Format("系统用户{0}不能删除", user.SystemName)); + + user.Deleted = true; + if (!String.IsNullOrEmpty(user.Email)) + user.Email += "-DELETED"; + if (!String.IsNullOrEmpty(user.Username)) + user.Username += "-DELETED"; + + UpdateUser(user); + } + + public User GetUserById(int userId) + { + if (userId == 0) + return null; + + return _useRepository.GetById(userId); + } + + public void InsertUser(User user) + { + if (user == null) throw new ArgumentNullException("user"); + + + + _useRepository.Insert(user); + + //event notification + //_eventPublisher.EntityInserted(customer); + } + + public void UpdateUser(User user) + { + if (user == null) + throw new ArgumentNullException("user"); + + _useRepository.Update(user); + + //还触发了事件通知! + //_eventPublisher.EntityUpdated(customer); + } + + public User GetUserByEmail(string email) + { + if (string.IsNullOrWhiteSpace(email)) return null; + + var query = from c in _useRepository.Table + orderby c.Id + where c.Email == email + select c; + + var user = query.FirstOrDefault(); + return user; + } + + public IList GetUsersByIds(int[] userIds) + { + if (userIds == null || userIds.Length == 0) + return new List(); + + var quey = _useRepository.Table.Where(n => userIds.Contains(n.Id)); + var users = quey.ToList(); + return userIds.Select(id => users.Find(x => x.Id == id)).Where(user => user != null).ToList(); + } + + public User GetUserByGuid(Guid userGuid) + { + return userGuid == Guid.Empty ? null : + _useRepository.Table.FirstOrDefault(n => n.UserGuid == userGuid); + } + + public User GetUserBySystemName(string systemName) + { + return string.IsNullOrWhiteSpace(systemName) ? null : _useRepository.Table.FirstOrDefault(n => n.SystemName == systemName); + } + + public User GetUserByUsername(string username) + { + return string.IsNullOrWhiteSpace(username) ? null : _useRepository.Table.FirstOrDefault(n => n.Username == username); + } + + + public IPagedList GetAllUsers(string email = null, string username = null, int pageIndex = 0, int pageSize = 2147483647) + { + var query = _useRepository.Table.Where(n => !n.Deleted); + if (!String.IsNullOrWhiteSpace(email)) + query = query.Where(c => c.Email.Contains(email)); + if (!String.IsNullOrWhiteSpace(username)) + query = query.Where(c => c.Email.Contains(username)); + query = query.OrderByDescending(c => c.CreateTime); + var users = new PagedList(query, pageIndex, pageSize); + return users; + } + + + public IPagedList GetOnlineUsers(DateTime lastActivityFromUtc, int[] userRoleIds, int pageIndex, int pageSize) + { + var query = _useRepository.Table; + query = query.Where(c => lastActivityFromUtc <= c.LastActivityDateUtc); + query = query.Where(c => !c.Deleted); + if (userRoleIds != null && userRoleIds.Length > 0) + query = query.Where(c => c.UserRoles.Select(cr => cr.Id).Intersect(userRoleIds).Any()); + + var users = new PagedList(query, pageIndex, pageSize); + return users; + } + + public virtual User InsertGuestUser() + { + var customer = new User + { + UserGuid = Guid.NewGuid(), + Active = true, + LastActivityDateUtc = DateTime.UtcNow, + }; + + //add to 'Guests' role + //var guestRole = GetUserRoleBySystemName(SystemUserRoleNames.Guests); + //if (guestRole == null) + // throw new PortalException("'Guests' role could not be loaded"); + //customer.UserRoles.Add(guestRole); + + // _useRepository.Insert(customer); + + return customer; + } + + + private const string UserrolesBySystemnameKey = "Portal.userrole.systemname-{0}"; + private const string UserrroleAllKey = "Portal.userrrole.all-{0}"; + private const string UserrrolePatternKey = "Portal.userrrole."; + + #region Role + + public virtual UserRole GetUserRoleBySystemName(string systemName) + { + if (String.IsNullOrWhiteSpace(systemName)) + return null; + var key = string.Format(UserrolesBySystemnameKey, systemName); + return _cacheManager.Get(key, () => + { + var query = from cr in _userRoleRepository.Table + orderby cr.Id + where cr.SystemName == systemName + select cr; + var customerRole = query.FirstOrDefault(); + return customerRole; + }); + } + + public void DeleteUserRole(UserRole role) + { + if (role == null) + throw new ArgumentNullException("role"); + + if (role.IsSystemRole) + throw new PortalException("系统用户不能删除"); + + _userRoleRepository.Delete(role); + + // _cacheManager.RemoveByPattern(UserrolesBySystemnameKey); + + // _eventPublisher.EntityDeleted(customerRole); + + } + + public UserRole GetUserRoleById(int userRoleId) + { + if (userRoleId == 0) + return null; + + return _userRoleRepository.GetById(userRoleId); + } + + /// + /// 缓存查询结果! + /// + /// + /// + public IList GetAllUserRoles(bool showHidden = false) + { + var query = from cr in _userRoleRepository.Table + orderby cr.Name + where (showHidden || cr.Active) + select cr; + return query.ToList(); + } + + public void InsertUserRole(UserRole userRole) + { + if (userRole == null) + throw new ArgumentNullException("userRole"); + + _userRoleRepository.Insert(userRole); + + //event notification + //_eventPublisher.EntityInserted(userRole); + } + + public void UpdateUserRole(UserRole userRole) + { + if (userRole == null) + throw new ArgumentNullException("userRole"); + + _userRoleRepository.Update(userRole); + + //event notification + // _eventPublisher.EntityUpdated(customerRole); + } + + + #endregion + } +} diff --git a/Niqiu.Core/bin/Debug/Niqiu.Core.dll b/Niqiu.Core/bin/Debug/Niqiu.Core.dll index 47c7eb4..094735e 100644 Binary files a/Niqiu.Core/bin/Debug/Niqiu.Core.dll and b/Niqiu.Core/bin/Debug/Niqiu.Core.dll differ diff --git a/Niqiu.Core/bin/Debug/Niqiu.Core.pdb b/Niqiu.Core/bin/Debug/Niqiu.Core.pdb index a7d86b1..5015c62 100644 Binary files a/Niqiu.Core/bin/Debug/Niqiu.Core.pdb and b/Niqiu.Core/bin/Debug/Niqiu.Core.pdb differ diff --git a/Niqiu.Core/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/Niqiu.Core/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 0c4c1fb..c325041 100644 Binary files a/Niqiu.Core/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/Niqiu.Core/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/Niqiu.Core/obj/Debug/Niqiu.Core.csproj.FileListAbsolute.txt b/Niqiu.Core/obj/Debug/Niqiu.Core.csproj.FileListAbsolute.txt index 4acc366..caac967 100644 --- a/Niqiu.Core/obj/Debug/Niqiu.Core.csproj.FileListAbsolute.txt +++ b/Niqiu.Core/obj/Debug/Niqiu.Core.csproj.FileListAbsolute.txt @@ -21,3 +21,14 @@ D:\VS2013\Portal.MVC\Portal.MVC\Niqiu.Core\bin\Debug\zh-Hans\EntityFramework.Sql D:\VS2013\Portal.MVC\Portal.MVC\Niqiu.Core\obj\Debug\Niqiu.Core.dll D:\VS2013\Portal.MVC\Portal.MVC\Niqiu.Core\obj\Debug\Niqiu.Core.pdb D:\VS2013\Portal.MVC\Portal.MVC\Niqiu.Core\obj\Debug\Niqiu.Core.csprojResolveAssemblyReference.cache +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\Niqiu.Core.dll +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\Niqiu.Core.pdb +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\EntityFramework.dll +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\EntityFramework.SqlServer.dll +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\EntityFramework.xml +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\EntityFramework.SqlServer.xml +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\zh-Hans\EntityFramework.resources.dll +D:\all in one\Git\Portal.MVC\Niqiu.Core\bin\Debug\zh-Hans\EntityFramework.SqlServer.resources.dll +D:\all in one\Git\Portal.MVC\Niqiu.Core\obj\Debug\Niqiu.Core.csprojResolveAssemblyReference.cache +D:\all in one\Git\Portal.MVC\Niqiu.Core\obj\Debug\Niqiu.Core.dll +D:\all in one\Git\Portal.MVC\Niqiu.Core\obj\Debug\Niqiu.Core.pdb diff --git a/Niqiu.Core/obj/Debug/Niqiu.Core.csprojResolveAssemblyReference.cache b/Niqiu.Core/obj/Debug/Niqiu.Core.csprojResolveAssemblyReference.cache index 1c5f08e..76eee36 100644 Binary files a/Niqiu.Core/obj/Debug/Niqiu.Core.csprojResolveAssemblyReference.cache and b/Niqiu.Core/obj/Debug/Niqiu.Core.csprojResolveAssemblyReference.cache differ diff --git a/Niqiu.Core/obj/Debug/Niqiu.Core.dll b/Niqiu.Core/obj/Debug/Niqiu.Core.dll index 47c7eb4..094735e 100644 Binary files a/Niqiu.Core/obj/Debug/Niqiu.Core.dll and b/Niqiu.Core/obj/Debug/Niqiu.Core.dll differ diff --git a/Niqiu.Core/obj/Debug/Niqiu.Core.pdb b/Niqiu.Core/obj/Debug/Niqiu.Core.pdb index a7d86b1..5015c62 100644 Binary files a/Niqiu.Core/obj/Debug/Niqiu.Core.pdb and b/Niqiu.Core/obj/Debug/Niqiu.Core.pdb differ diff --git a/Portal.MVC.v12.suo b/Portal.MVC.v12.suo index 429c65f..f104c59 100644 Binary files a/Portal.MVC.v12.suo and b/Portal.MVC.v12.suo differ diff --git a/Portal.MVC/Web.config b/Portal.MVC/Web.config index 30dc9cc..3dc9cc3 100644 --- a/Portal.MVC/Web.config +++ b/Portal.MVC/Web.config @@ -1,129 +1,129 @@ - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Portal.MVC/bin/Niqiu.Core.dll b/Portal.MVC/bin/Niqiu.Core.dll index 47c7eb4..094735e 100644 Binary files a/Portal.MVC/bin/Niqiu.Core.dll and b/Portal.MVC/bin/Niqiu.Core.dll differ diff --git a/Portal.MVC/bin/Niqiu.Core.pdb b/Portal.MVC/bin/Niqiu.Core.pdb index a7d86b1..5015c62 100644 Binary files a/Portal.MVC/bin/Niqiu.Core.pdb and b/Portal.MVC/bin/Niqiu.Core.pdb differ diff --git a/Portal.MVC/bin/Portal.MVC.dll b/Portal.MVC/bin/Portal.MVC.dll index ae53246..3931031 100644 Binary files a/Portal.MVC/bin/Portal.MVC.dll and b/Portal.MVC/bin/Portal.MVC.dll differ diff --git a/Portal.MVC/bin/Portal.MVC.dll.config b/Portal.MVC/bin/Portal.MVC.dll.config index 30dc9cc..d4bae1b 100644 --- a/Portal.MVC/bin/Portal.MVC.dll.config +++ b/Portal.MVC/bin/Portal.MVC.dll.config @@ -12,7 +12,7 @@ - + @@ -22,12 +22,12 @@ - + - + - + @@ -77,11 +77,11 @@ - + - + @@ -118,8 +118,8 @@ --> - -