public Comment WriteComment(int NewsId, User user, string comment, Nullable <int> parentId) { if (user == null) { throw new Exception("请登陆后再评论"); } else if (comment == null) { throw new Exception("评论不能为空"); } else if (user != null && comment != null) { Comment com = new Comment() { NewsId = NewsId, CommentContent = comment, CommentPerson = user.UserAccout, CommentTime = DateTime.Now, ParentId = parentId, IsDelete = false, IsPublic = false }; _ctx.Comment.Add(com); _ctx.SaveChanges(); News news = _ctx.News.Find(NewsId); news.CommentNumber++; _ctx.SaveChanges(); return(com); } else { throw new Exception("未知错误"); } }
public User AddUser(string userAccount, string password, Nullable <bool> isAdmin) { User user = _ctx.User.SingleOrDefault(u => u.UserAccout == userAccount); if (user == null) { User people = new User { UserAccout = userAccount, Password = password, IsDelet = false }; if (isAdmin == false || isAdmin == null) { people.IsAdmin = false; } else { people.IsAdmin = true; } _ctx.User.Add(people); _ctx.SaveChanges(); return(user); } else { throw new Exception("该手机号已经注册"); } }
public Comment CommentRecover(int id) { Comment com = _ctx.Comment.SingleOrDefault(c => c.Id == id); com.IsDelete = false; com.IsPublic = false; _ctx.SaveChanges(); return(com); }
private void CreateEditions() { var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName); if (defaultEdition == null) { defaultEdition = new Edition { Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName }; _context.Editions.Add(defaultEdition); _context.SaveChanges(); /* Add desired features to the standard edition, if wanted... */ } }
public void Create() { new DefaultEditionCreator(_context).Create(); new DefaultLanguagesCreator(_context).Create(); new HostRoleAndUserCreator(_context).Create(); new DefaultSettingsCreator(_context).Create(); _context.SaveChanges(); }
private void AddSettingIfNotExists(string name, string value, int?tenantId = null) { if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null)) { return; } _context.Settings.Add(new Setting(tenantId, null, name, value)); _context.SaveChanges(); }
private void AddLanguageIfNotExists(ApplicationLanguage language) { if (_context.Languages.IgnoreQueryFilters().Any(l => l.TenantId == language.TenantId && l.Name == language.Name)) { return; } _context.Languages.Add(language); _context.SaveChanges(); }
private static void BuildDefaultHostTactic(MESDbContext context) { if (!context.Tactic.Any(o => o.TacticName == "注册提醒" && o.TenantId == null)) { //注册提醒 var tactic = new Tactic() { TenantId = null, TacticName = "注册提醒", TacticType = TacticType.Host, IsActive = true }; context.Tactic.Add(tactic); context.SaveChanges(); } }
private void CreateDefaultTenant() { // Default tenant var defaultTenant = _context.Tenants.IgnoreQueryFilters().FirstOrDefault(t => t.TenancyName == AbpTenantBase.DefaultTenantName); if (defaultTenant == null) { defaultTenant = new Tenant(AbpTenantBase.DefaultTenantName, AbpTenantBase.DefaultTenantName); var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName); if (defaultEdition != null) { defaultTenant.EditionId = defaultEdition.Id; } _context.Tenants.Add(defaultTenant); _context.SaveChanges(); } }
////增删改查 public News AddNews(string content, string title, string firstType, string secondType, string writer) { List <string> imgurl = GetImages(content); News news = new News { CommentNumber = 0, Content = content, FirstType = firstType, SecondType = secondType, UpTime = DateTime.Now, Title = title, Writer = writer, IsDelet = false, IsPublish = false }; if (imgurl.Count == 0) { news.CheckImage = false; } else { news.CheckImage = true; } var findNews = _ctx.News.SingleOrDefault(f => f.Title == title); if (findNews != null) { throw new Exception("该标题已经被上传"); } else { _ctx.News.Add(news); _ctx.SaveChanges(); return(news); //throw new Exception("上传新闻成功,正在等待审核"); } }
private void CreateRolesAndUsers() { // Admin role var adminRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin); if (adminRole == null) { adminRole = _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin) { IsStatic = true }).Entity; _context.SaveChanges(); } // Grant all permissions to admin role var grantedPermissions = _context.Permissions.IgnoreQueryFilters() .OfType <RolePermissionSetting>() .Where(p => p.TenantId == _tenantId && p.RoleId == adminRole.Id) .Select(p => p.Name) .ToList(); var permissions = PermissionFinder .GetAllPermissions(new MESAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant) && !grantedPermissions.Contains(p.Name)) .ToList(); if (permissions.Any()) { _context.Permissions.AddRange( permissions.Select(permission => new RolePermissionSetting { TenantId = _tenantId, Name = permission.Name, IsGranted = true, RoleId = adminRole.Id }) ); _context.SaveChanges(); } // Admin user var adminUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == AbpUserBase.AdminUserName); if (adminUser == null) { adminUser = User.CreateTenantAdminUser(_tenantId, "*****@*****.**"); adminUser.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUser, "123qwe"); adminUser.IsEmailConfirmed = true; adminUser.IsActive = true; _context.Users.Add(adminUser); _context.SaveChanges(); // Assign Admin role to admin user _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id)); _context.SaveChanges(); } }
private void CreateHostRoleAndUsers() { // Admin role for host var adminRoleForHost = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin); if (adminRoleForHost == null) { adminRoleForHost = _context.Roles.Add(new Role(null, StaticRoleNames.Host.Admin, StaticRoleNames.Host.Admin) { IsStatic = true, IsDefault = true }).Entity; _context.SaveChanges(); } // Grant all permissions to admin role for host var grantedPermissions = _context.Permissions.IgnoreQueryFilters() .OfType <RolePermissionSetting>() .Where(p => p.TenantId == null && p.RoleId == adminRoleForHost.Id) .Select(p => p.Name) .ToList(); var permissions = PermissionFinder .GetAllPermissions(new MESAuthorizationProvider()) .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host) && !grantedPermissions.Contains(p.Name)) .ToList(); if (permissions.Any()) { _context.Permissions.AddRange( permissions.Select(permission => new RolePermissionSetting { TenantId = null, Name = permission.Name, IsGranted = true, RoleId = adminRoleForHost.Id }) ); _context.SaveChanges(); } // Admin user for host var adminUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == AbpUserBase.AdminUserName); if (adminUserForHost == null) { var user = new User { TenantId = null, UserName = AbpUserBase.AdminUserName, Name = "admin", Surname = "admin", EmailAddress = "*****@*****.**", IsEmailConfirmed = true, IsActive = true }; user.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, "123qwe"); user.SetNormalizedNames(); adminUserForHost = _context.Users.Add(user).Entity; _context.SaveChanges(); // Assign Admin role to admin user _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id)); _context.SaveChanges(); _context.SaveChanges(); } }