Ejemplo n.º 1
0
        private void AddLanguageIfNotExists(ApplicationLanguage language)
        {
            if (_context.Languages.Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                return;
            }

            _context.Languages.Add(language);

            _context.SaveChanges();
        }
Ejemplo n.º 2
0
        private void CreateEditions()
        {
            var defaultEdition = _context.Editions.FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);

            if (defaultEdition == null)
            {
                defaultEdition = new Edition {
                    Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName
                };
                _context.Editions.Add(defaultEdition);
                _context.SaveChanges();

                //TODO: Add desired features to the standard edition, if wanted!
            }
        }
        private void CreateUserAndRoles()
        {
            //添加 租主管理员角色 静态(该角色不允许更改权限)
            var adminRoleForHost = _context.Roles.FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin);

            if (adminRoleForHost == null)
            {
                adminRoleForHost = _context.Roles.Add(new Role {
                    Name = StaticRoleNames.Host.Admin, DisplayName = StaticRoleNames.Host.Admin, IsStatic = true
                });
                _context.SaveChanges();
            }

            //分配所有 租主权限 给租主管理员角色
            var permissions = PermissionFinder
                              .GetAllPermissions(new SpartanAuthorizationProvider())
                              .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                              .ToList();

            foreach (var permission in permissions)
            {
                if (!permission.IsGrantedByDefault)
                {
                    var permissionSetting =
                        _context.RolePermissions.FirstOrDefault(
                            p => p.Name == permission.Name && p.IsGranted && p.RoleId == adminRoleForHost.Id);

                    if (permissionSetting == null)
                    {
                        _context.Permissions.Add(
                            new RolePermissionSetting
                        {
                            Name      = permission.Name,
                            IsGranted = true,
                            RoleId    = adminRoleForHost.Id
                        });
                    }
                }
            }
            _context.SaveChanges();

            //添加一个租主
            var adminUserForHost = _context.Users.FirstOrDefault(u => u.TenantId == null && u.UserName == User.AdminUserName);

            if (adminUserForHost == null)
            {
                adminUserForHost = _context.Users.Add(
                    new User
                {
                    TenantId         = null,
                    UserName         = User.AdminUserName,
                    Name             = User.AdminUserName,
                    Surname          = "管理员",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    Password         = new Md532PasswordHasher().HashPassword(SpartanConsts.DefaultPassword)
                });

                _context.SaveChanges();
                //给租主 赋予租主管理员角色
                _context.UserRoles.Add(new UserRole(adminUserForHost.Id, adminRoleForHost.Id));
                _context.SaveChanges();
            }


            //给所有租户管理员角色 添加权限
            var tenantAdminRoles =
                _context.Roles.Where(p => p.Name == StaticRoleNames.Tenants.Admin && p.TenantId != null).ToList();

            permissions = PermissionFinder
                          .GetAllPermissions(new SpartanAuthorizationProvider())
                          .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                          .ToList();

            foreach (var tenantAdminRole in tenantAdminRoles)
            {
                foreach (var permission in permissions)
                {
                    if (!permission.IsGrantedByDefault)
                    {
                        var permissionSetting =
                            _context.RolePermissions.FirstOrDefault(
                                p => p.Name == permission.Name && p.IsGranted && p.RoleId == tenantAdminRole.Id);

                        if (permissionSetting == null)
                        {
                            _context.Permissions.Add(
                                new RolePermissionSetting
                            {
                                Name      = permission.Name,
                                IsGranted = true,
                                RoleId    = tenantAdminRole.Id
                            });
                        }
                    }
                }
                _context.SaveChanges();
            }
        }