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 permissions = PermissionFinder
                                  .GetAllPermissions(new saasAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        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();

                // User account of admin user
                if (_tenantId == 1)
                {
                    _context.UserAccounts.Add(new UserAccount
                    {
                        TenantId     = _tenantId,
                        UserId       = adminUser.Id,
                        UserName     = AbpUserBase.AdminUserName,
                        EmailAddress = adminUser.EmailAddress
                    });
                    _context.SaveChanges();
                }
            }
        }
예제 #2
0
        private void CreateHostAndUsers()
        {
            //Admin role for host

            var adminRoleForHost = _context.Roles.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
                });
                _context.SaveChanges();
            }

            //admin user for host

            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             = "admin",
                    Surname          = "admin",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    ShouldChangePasswordOnNextLogin = true,
                    IsActive = true,
                    Password = "******"     //123qwe
                });
                _context.SaveChanges();

                //Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(adminUserForHost.Id, adminRoleForHost.Id));
                _context.SaveChanges();

                //Grant all permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new AppAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    if (!permission.IsGrantedByDefault)
                    {
                        _context.Permissions.Add(
                            new RolePermissionSetting
                        {
                            Name      = permission.Name,
                            IsGranted = true,
                            RoleId    = adminRoleForHost.Id
                        });
                    }
                }

                _context.SaveChanges();
            }
        }
예제 #3
0
        private void CreateHostRoleAndUsers()
        {
            //Admin role for host

            var adminRoleForHost = _context.Roles.FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin);

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

                adminRoleForHost.SetNormalizedName();

                _context.Roles.Add(adminRoleForHost);
                _context.SaveChanges();

                //Grant all tenant permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new TestProjectAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRoleForHost.Id
                    });
                }

                _context.SaveChanges();
            }

            //Admin user for tenancy host

            var adminUserForHost = _context.Users.FirstOrDefault(u => u.TenantId == null && u.UserName == User.AdminUserName);

            if (adminUserForHost == null)
            {
                adminUserForHost = _context.Users.Add(
                    new User
                {
                    UserName         = User.AdminUserName,
                    Name             = "System",
                    Surname          = "Administrator",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    Password         = new PasswordHasher().HashPassword(User.DefaultPassword)
                });

                adminUserForHost.SetNormalizedNames();

                _context.SaveChanges();

                _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.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 MahjongAuthorizationProvider())
                              .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();
            }
        }
        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 NovusAuthorizationProvider())
                              .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,
                    Password         = "******" // 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();

                // User account of admin user
                _context.UserAccounts.Add(new UserAccount
                {
                    TenantId     = null,
                    UserId       = adminUserForHost.Id,
                    UserName     = AbpUserBase.AdminUserName,
                    EmailAddress = adminUserForHost.EmailAddress
                });
                _context.SaveChanges();
            }
        }
예제 #6
0
        private void CreateUserAndRoles()
        {
            //Admin role for host

            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();

                //Grant all tenant permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new MahjongBuddyAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    if (!permission.IsGrantedByDefault)
                    {
                        _context.Permissions.Add(
                            new RolePermissionSetting
                        {
                            Name      = permission.Name,
                            IsGranted = true,
                            RoleId    = adminRoleForHost.Id
                        });
                    }
                }

                _context.SaveChanges();
            }

            //Admin user for tenancy host

            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             = "System",
                    Surname          = "Administrator",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    Password         = new PasswordHasher().HashPassword(User.DefaultPassword)
                });

                _context.SaveChanges();

                _context.UserRoles.Add(new UserRole(adminUserForHost.Id, adminRoleForHost.Id));

                _context.SaveChanges();
            }

            //Default tenant

            var defaultTenant = _context.Tenants.FirstOrDefault(t => t.TenancyName == "Default");

            if (defaultTenant == null)
            {
                defaultTenant = _context.Tenants.Add(new Tenant {
                    TenancyName = "Default", Name = "Default"
                });
                _context.SaveChanges();
            }

            //Admin role for 'Default' tenant

            var adminRoleForDefaultTenant = _context.Roles.FirstOrDefault(r => r.TenantId == defaultTenant.Id && r.Name == StaticRoleNames.Tenants.Admin);

            if (adminRoleForDefaultTenant == null)
            {
                adminRoleForDefaultTenant = _context.Roles.Add(new Role {
                    TenantId = defaultTenant.Id, Name = StaticRoleNames.Tenants.Admin, DisplayName = StaticRoleNames.Tenants.Admin, IsStatic = true
                });
                _context.SaveChanges();

                //Grant all tenant permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new MahjongBuddyAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    if (!permission.IsGrantedByDefault)
                    {
                        _context.Permissions.Add(
                            new RolePermissionSetting
                        {
                            Name      = permission.Name,
                            IsGranted = true,
                            RoleId    = adminRoleForDefaultTenant.Id
                        });
                    }
                }

                _context.SaveChanges();
            }

            //Admin for 'Default' tenant

            var adminUserForDefaultTenant = _context.Users.FirstOrDefault(u => u.TenantId == defaultTenant.Id && u.UserName == User.AdminUserName);

            if (adminUserForDefaultTenant == null)
            {
                adminUserForDefaultTenant = _context.Users.Add(
                    new User
                {
                    TenantId         = defaultTenant.Id,
                    UserName         = User.AdminUserName,
                    Name             = "System",
                    Surname          = "Administrator",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    Password         = new PasswordHasher().HashPassword(User.DefaultPassword)
                });
                _context.SaveChanges();

                _context.UserRoles.Add(new UserRole(adminUserForDefaultTenant.Id, adminRoleForDefaultTenant.Id));
                _context.SaveChanges();
            }
        }
예제 #7
0
        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();
            }

            //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,
                    ShouldChangePasswordOnNextLogin = true,
                    IsActive = true,
                    Password = "******" //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();

                //Grant all permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new AppAuthorizationProvider(true))
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        TenantId  = null,
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRoleForHost.Id
                    });
                }

                _context.SaveChanges();

                //User account of admin user
                _context.UserAccounts.Add(new UserAccount
                {
                    TenantId     = null,
                    UserId       = adminUserForHost.Id,
                    UserName     = AbpUserBase.AdminUserName,
                    EmailAddress = adminUserForHost.EmailAddress
                });

                _context.SaveChanges();

                //Notification subscriptions
                _context.NotificationSubscriptions.Add(new NotificationSubscriptionInfo(SequentialGuidGenerator.Instance.Create(), null, adminUserForHost.Id, AppNotificationNames.NewTenantRegistered));
                _context.NotificationSubscriptions.Add(new NotificationSubscriptionInfo(SequentialGuidGenerator.Instance.Create(), null, adminUserForHost.Id, AppNotificationNames.NewUserRegistered));

                _context.SaveChanges();
            }
        }
예제 #8
0
        private void CreateHostRoleAndUsers()
        {
            // 初始化组织
            var adminOrgForHost = _context.Orgs.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null && r.Code == StaticRoleNames.Host.Org);

            if (adminOrgForHost == null)
            {
                adminOrgForHost = _context.Orgs.Add(new Entities.Org()
                {
                    Code = StaticRoleNames.Host.Org, IsActive = true
                }).Entity;
                _context.SaveChanges();
            }

            // 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,
                    OrgId     = adminOrgForHost.Id
                }).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 WMSCloudAuthorizationProvider())
                              .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();

                // User account of admin user
                _context.UserAccounts.Add(new UserAccount
                {
                    TenantId     = null,
                    UserId       = adminUserForHost.Id,
                    UserName     = AbpUserBase.AdminUserName,
                    EmailAddress = adminUserForHost.EmailAddress
                });
                _context.SaveChanges();
            }


            // 初始化菜单
            var adminMenuForHost = _context.Menus.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null);

            if (adminMenuForHost == null)
            {
                adminMenuForHost = _context.Menus.Add(new Menu()
                {
                    Text          = "主菜单",
                    Group         = true,
                    I18n          = "主菜单",
                    IsActive      = true,
                    Acl           = PermissionNames.Pages_MainMenu,
                    CreatorUserId = adminUserForHost.Id
                }).Entity;

                adminMenuForHost = _context.Menus.Add(new Menu()
                {
                    Text          = "系统维护",
                    I18n          = "系统维护",
                    Icon          = "icon-settings",
                    ParentId      = adminMenuForHost.Id,
                    Acl           = PermissionNames.Pages_Sys,
                    IsActive      = true,
                    CreatorUserId = adminUserForHost.Id
                }).Entity;

                _context.Menus.Add(new Menu()
                {
                    Text          = "用户管理",
                    I18n          = "用户管理",
                    Acl           = PermissionNames.Pages_Users,
                    Link          = "/sys/user",
                    ParentId      = adminMenuForHost.Id,
                    IsActive      = true,
                    CreatorUserId = adminUserForHost.Id
                });
                _context.Menus.Add(new Menu()
                {
                    Text          = "组织管理",
                    I18n          = "组织管理",
                    Acl           = PermissionNames.Pages_Orgs,
                    Link          = "/sys/org",
                    ParentId      = adminMenuForHost.Id,
                    IsActive      = true,
                    CreatorUserId = adminUserForHost.Id
                });
                _context.Menus.Add(new Menu()
                {
                    Text          = "菜单管理",
                    I18n          = "菜单管理",
                    Acl           = PermissionNames.Pages_Menus,
                    Link          = "/sys/menu",
                    ParentId      = adminMenuForHost.Id,
                    IsActive      = true,
                    CreatorUserId = adminUserForHost.Id
                });
                _context.Menus.Add(new Menu()
                {
                    Text          = "角色管理",
                    I18n          = "角色管理",
                    Acl           = PermissionNames.Pages_Roles,
                    Link          = "/sys/role",
                    ParentId      = adminMenuForHost.Id,
                    IsActive      = true,
                    CreatorUserId = adminUserForHost.Id
                });
                _context.Menus.Add(new Menu()
                {
                    Text          = "租户管理",
                    I18n          = "租户管理",
                    Acl           = PermissionNames.Pages_Tenants,
                    Link          = "/sys/tenant",
                    ParentId      = adminMenuForHost.Id,
                    IsActive      = true,
                    CreatorUserId = adminUserForHost.Id
                });

                _context.SaveChanges();
            }
        }
        private void CreateRolesAndUsers()
        {
            //Admin role

            var adminRole = _context.Roles.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 permissions = PermissionFinder
                                  .GetAllPermissions(new AppAuthorizationProvider(false))
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        TenantId  = _tenantId,
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRole.Id
                    });
                }

                _context.SaveChanges();
            }

            //User role

            var userRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.User);

            if (userRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.User, StaticRoleNames.Tenants.User)
                {
                    IsStatic = true, IsDefault = true
                });
                _context.SaveChanges();
            }

            //admin user

            var adminUser = _context.Users.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.ShouldChangePasswordOnNextLogin = 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();

                //User account of admin user
                if (_tenantId == 1)
                {
                    _context.UserAccounts.Add(new UserAccount
                    {
                        TenantId     = _tenantId,
                        UserId       = adminUser.Id,
                        UserName     = AbpUserBase.AdminUserName,
                        EmailAddress = adminUser.EmailAddress
                    });
                    _context.SaveChanges();
                }

                //Notification subscription
                _context.NotificationSubscriptions.Add(new NotificationSubscriptionInfo(SequentialGuidGenerator.Instance.Create(), _tenantId, adminUser.Id, AppNotificationNames.NewUserRegistered));
                _context.SaveChanges();
            }
        }
예제 #10
0
        private void CreateRolesAndUsers()
        {
            //Admin role

            var adminRole = _context.Roles.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
                });
                _context.SaveChanges();

                //Grant all permissions to admin role
                var permissions = PermissionFinder
                                  .GetAllPermissions(new AppAuthorizationProvider(false))
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        TenantId  = _tenantId,
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRole.Id
                    });
                }

                _context.SaveChanges();
            }

            //User role

            var userRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.User);

            if (userRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.User, StaticRoleNames.Tenants.User)
                {
                    IsStatic = true, IsDefault = true
                });
                _context.SaveChanges();
            }

            //admin user

            var adminUser = _context.Users.FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == User.AdminUserName);

            if (adminUser == null)
            {
                adminUser = User.CreateTenantAdminUser(_tenantId, "*****@*****.**", "123qwe");
                adminUser.IsEmailConfirmed = true;
                adminUser.ShouldChangePasswordOnNextLogin = 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();

                //User account of admin user
                if (_tenantId == 1)
                {
                    _context.UserAccounts.Add(new UserAccount
                    {
                        TenantId     = _tenantId,
                        UserId       = adminUser.Id,
                        UserName     = User.AdminUserName,
                        EmailAddress = adminUser.EmailAddress
                    });
                    _context.SaveChanges();
                }
            }
        }
        private void CreateHostRoleAndUsers()
        {
            //Admin role for host

            var adminRoleForHost = _context.Roles.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
                });
                _context.SaveChanges();

                //Grant all tenant permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new ModuleZeroSampleProjectAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRoleForHost.Id
                    });
                }

                _context.SaveChanges();
            }

            //Admin user for tenancy host

            var adminUserForHost = _context.Users.FirstOrDefault(u => u.TenantId == null && u.UserName == User.AdminUserName);

            if (adminUserForHost == null)
            {
                adminUserForHost = _context.Users.Add(
                    new User
                {
                    UserName         = AbpUserBase.AdminUserName,
                    Name             = "System",
                    Surname          = "Administrator",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    Password         = "******", //123qwe
                    IsActive         = true,
                    TenantId         = null
                });

                adminUserForHost.SetNormalizedNames();

                _context.SaveChanges();

                _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id));

                _context.SaveChanges();
            }
        }
        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();
            }
        }
예제 #13
0
        private void CreateHostRoleAndUsers()
        {
            // System role and user for host
            CreateRoleAndDefaulUser(StaticRoleNames.Host.System, PermissionNames.System, StaticUserNames.System);

            // 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();
            }

            // 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,
                    Password         = "******" // 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();

                // Grant all permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new ChiMaAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        TenantId  = null,
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRoleForHost.Id
                    });
                }

                _context.SaveChanges();

                // User account of admin user
                _context.UserAccounts.Add(new UserAccount
                {
                    TenantId     = null,
                    UserId       = adminUserForHost.Id,
                    UserName     = AbpUserBase.AdminUserName,
                    EmailAddress = adminUserForHost.EmailAddress
                });

                _context.SaveChanges();
            }
        }
예제 #14
0
        private void CreateDefaultTenantAndUsers()
        {
            //Default tenant

            var defaultTenant = _context.Tenants.FirstOrDefault(t => t.TenancyName == Tenant.DefaultTenantName);

            if (defaultTenant == null)
            {
                defaultTenant = new Tenant(Tenant.DefaultTenantName, Tenant.DefaultTenantName);

                var defaultEdition = _context.Editions.FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);
                if (defaultEdition != null)
                {
                    defaultTenant.EditionId = defaultEdition.Id;
                }

                defaultTenant = _context.Tenants.Add(defaultTenant);
                _context.SaveChanges();
            }

            //Admin role for 'Default' tenant

            var adminRoleForDefaultTenant = _context.Roles.FirstOrDefault(r => r.TenantId == defaultTenant.Id && r.Name == StaticRoleNames.Tenants.Admin);

            if (adminRoleForDefaultTenant == null)
            {
                adminRoleForDefaultTenant = _context.Roles.Add(new Role(defaultTenant.Id, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin)
                {
                    IsStatic = true
                });
                _context.SaveChanges();
            }

            //User role for 'Default' tenant

            var userRoleForDefaultTenant = _context.Roles.FirstOrDefault(r => r.TenantId == defaultTenant.Id && r.Name == StaticRoleNames.Tenants.User);

            if (userRoleForDefaultTenant == null)
            {
                _context.Roles.Add(new Role(defaultTenant.Id, StaticRoleNames.Tenants.User, StaticRoleNames.Tenants.User)
                {
                    IsStatic = true, IsDefault = true
                });
                _context.SaveChanges();
            }

            //admin user for 'Default' tenant

            var adminUserForDefaultTenant = _context.Users.FirstOrDefault(u => u.TenantId == defaultTenant.Id && u.UserName == User.AdminUserName);

            if (adminUserForDefaultTenant == null)
            {
                adminUserForDefaultTenant = User.CreateTenantAdminUser(defaultTenant.Id, "*****@*****.**", "123qwe");
                adminUserForDefaultTenant.IsEmailConfirmed = true;
                adminUserForDefaultTenant.ShouldChangePasswordOnNextLogin = true;
                adminUserForDefaultTenant.IsActive = true;

                _context.Users.Add(adminUserForDefaultTenant);
                _context.SaveChanges();

                //Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(adminUserForDefaultTenant.Id, adminRoleForDefaultTenant.Id));
                _context.SaveChanges();

                //Grant all permissions
                var permissions = PermissionFinder
                                  .GetAllPermissions(new AppAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    if (!permission.IsGrantedByDefault)
                    {
                        _context.Permissions.Add(
                            new RolePermissionSetting
                        {
                            Name      = permission.Name,
                            IsGranted = true,
                            RoleId    = adminRoleForDefaultTenant.Id
                        });
                    }
                }

                _context.SaveChanges();
            }
        }
예제 #15
0
        private void CreateRolesAndUsers()
        {
            //Admin role

            var adminRole = _context.Roles.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, RoleCategoryID = 1
                });
                _context.SaveChanges();

                //Grant all permissions to admin role
                var permissions = PermissionFinder
                                  .GetAllPermissions(new AppAuthorizationProvider(false))
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        TenantId  = _tenantId,
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRole.Id
                    });
                }

                _context.SaveChanges();
            }

            //User role

            var userRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.User);

            if (userRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.User, StaticRoleNames.Tenants.User)
                {
                    IsStatic = true, IsDefault = true, RoleCategoryID = 2
                });
                _context.SaveChanges();
            }

            var Claims_HandlerRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Claims_Handler);

            if (Claims_HandlerRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Claims_Handler, StaticRoleNames.Tenants.Claims_Handler)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 3
                });
                _context.SaveChanges();
            }

            var CSARole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.CSA);

            if (CSARole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.CSA, StaticRoleNames.Tenants.CSA)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 4
                });
                _context.SaveChanges();
            }

            var PartsBuyerRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Parts_Buyer);

            if (PartsBuyerRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Parts_Buyer, StaticRoleNames.Tenants.Parts_Buyer)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 5
                });
                _context.SaveChanges();
            }

            var EstimatorRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Estimator);

            if (EstimatorRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Estimator, StaticRoleNames.Tenants.Estimator)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 6
                });
                _context.SaveChanges();
            }

            var KeyAccountManagerRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Key_Accounts_Manager);

            if (KeyAccountManagerRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Key_Accounts_Manager, StaticRoleNames.Tenants.Key_Accounts_Manager)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 7
                });
                _context.SaveChanges();
            }

            var SwithchboardRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Swithchboard);

            if (SwithchboardRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Swithchboard, StaticRoleNames.Tenants.Swithchboard)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 8
                });
                _context.SaveChanges();
            }

            var PartsReceiverRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Parts_Receiver);

            if (PartsReceiverRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Parts_Receiver, StaticRoleNames.Tenants.Parts_Receiver)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 9
                });
                _context.SaveChanges();
            }

            var CostingClerkRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Costing_Clerk);

            if (CostingClerkRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Costing_Clerk, StaticRoleNames.Tenants.Costing_Clerk)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 10
                });
                _context.SaveChanges();
            }

            var FinancialManagerRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Financial_Manager);

            if (FinancialManagerRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Financial_Manager, StaticRoleNames.Tenants.Financial_Manager)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 11
                });
                _context.SaveChanges();
            }

            var InsurerRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Insurer);

            if (InsurerRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Insurer, StaticRoleNames.Tenants.Insurer)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 12
                });
                _context.SaveChanges();
            }

            var BrokerRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Broker);

            if (BrokerRole == null)
            {
                _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Broker, StaticRoleNames.Tenants.Broker)
                {
                    IsStatic = true, IsDefault = false, RoleCategoryID = 13
                });
                _context.SaveChanges();
            }

            //admin user

            var adminUser = _context.Users.FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == User.AdminUserName);

            if (adminUser == null)
            {
                adminUser = User.CreateTenantAdminUser(_tenantId, "*****@*****.**", "123qwe");
                adminUser.IsEmailConfirmed = true;
                adminUser.ShouldChangePasswordOnNextLogin = 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();

                //User account of admin user
                if (_tenantId == 1)
                {
                    _context.UserAccounts.Add(new UserAccount
                    {
                        TenantId     = _tenantId,
                        UserId       = adminUser.Id,
                        UserName     = User.AdminUserName,
                        EmailAddress = adminUser.EmailAddress
                    });
                    _context.SaveChanges();
                }
            }
        }
예제 #16
0
        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 LMSAuthorizationProvider())
                              .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();
            }
        }
예제 #17
0
        private void CreateRolesAndUsers()
        {
            // 初始化组织
            var adminOrgForHost = _context.Orgs.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Code == StaticRoleNames.Host.Org);

            if (adminOrgForHost == null)
            {
                adminOrgForHost = _context.Orgs.Add(new Entities.Org()
                {
                    Code = StaticRoleNames.Host.Org, TenantId = _tenantId, IsActive = true
                }).Entity;
                _context.SaveChanges();
            }

            // 初始化菜单
            var adminMenuForHost = _context.Menus.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId);

            if (adminMenuForHost == null)
            {
                adminMenuForHost = _context.Menus.Add(new Entities.Menu()
                {
                    Name      = "主菜单",
                    Group     = true,
                    Translate = "主菜单",
                    TenantId  = _tenantId,
                    IsActive  = true
                }).Entity;

                adminMenuForHost = _context.Menus.Add(new Entities.Menu()
                {
                    Name      = "系统维护",
                    Translate = "系统维护",
                    Icon      = "icon-settings",
                    ParentId  = adminMenuForHost.Id,
                    TenantId  = _tenantId,
                    IsActive  = true
                }).Entity;

                _context.Menus.Add(new Entities.Menu()
                {
                    Name = "用户管理", Translate = "用户管理", TenantId = _tenantId, Link = PermissionNames.Pages_Users, ParentId = adminMenuForHost.Id, IsActive = true
                });
                _context.Menus.Add(new Entities.Menu()
                {
                    Name = "组织管理", Translate = "组织管理", TenantId = _tenantId, Link = PermissionNames.Pages_Orgs, ParentId = adminMenuForHost.Id, IsActive = true
                });
                _context.Menus.Add(new Entities.Menu()
                {
                    Name = "菜单管理", Translate = "菜单管理", TenantId = _tenantId, Link = PermissionNames.Pages_Menus, ParentId = adminMenuForHost.Id, IsActive = true
                });
                _context.Menus.Add(new Entities.Menu()
                {
                    Name = "角色管理", Translate = "角色管理", TenantId = _tenantId, Link = PermissionNames.Pages_Roles, ParentId = adminMenuForHost.Id, IsActive = true
                });
                _context.SaveChanges();
            }


            // Admin role

            var adminRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == adminOrgForHost.Id + "_" + StaticRoleNames.Tenants.Admin);

            if (adminRole == null)
            {
                adminRole = _context.Roles.Add(new Role(_tenantId, adminOrgForHost.Id + "_" + StaticRoleNames.Tenants.Admin, adminOrgForHost.Id + "_" + StaticRoleNames.Tenants.Admin)
                {
                    IsStatic = true, OrgId = adminOrgForHost.Id, IsActive = true
                }).Entity;
                _context.SaveChanges();

                // Grant all permissions to admin role
                var permissions = PermissionFinder
                                  .GetAllPermissions(new MESCloudAuthorizationProvider())
                                  .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant))
                                  .ToList();

                foreach (var permission in permissions)
                {
                    _context.Permissions.Add(
                        new RolePermissionSetting
                    {
                        TenantId  = _tenantId,
                        Name      = permission.Name,
                        IsGranted = true,
                        RoleId    = adminRole.Id
                    });
                }

                _context.SaveChanges();
            }
            // 添加菜单权限
            var adminMRForHost = _context.MenuRoleMap.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.RoleId == adminRole.Id);

            if (adminMRForHost == null)
            {
                List <MenuRoleMap> mr = new List <MenuRoleMap>();
                foreach (var item in _context.Menus.IgnoreQueryFilters().Where(r => r.TenantId == _tenantId).Select(r => r.Id))
                {
                    mr.Add(new MenuRoleMap()
                    {
                        MenuId = item, RoleId = adminRole.Id, TenantId = _tenantId
                    });
                }
                _context.MenuRoleMap.AddRange(mr.ToArray());
                _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();

                // User account of admin user
                if (_tenantId == 1)
                {
                    _context.UserAccounts.Add(new UserAccount
                    {
                        TenantId     = _tenantId,
                        UserId       = adminUser.Id,
                        UserName     = AbpUserBase.AdminUserName,
                        EmailAddress = adminUser.EmailAddress
                    });
                    _context.SaveChanges();
                }
            }

            // 初始化角色菜单权限
            var adminRoleMenu = _context.MenuRoleMap.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.RoleId == adminRole.Id);

            if (adminRoleMenu == null)
            {
                foreach (var item in _context.Menus.Where(m => m.TenantId == _tenantId))
                {
                    _context.MenuRoleMap.Add(new Entities.MenuRoleMap()
                    {
                        MenuId = item.Id, RoleId = adminRole.Id, TenantId = _tenantId, CreatorUserId = adminUser.Id, CreationTime = DateTime.Now
                    });
                }
                _context.SaveChanges();
            }
        }