예제 #1
0
        public static void Initialize(ApplicationDbContext context)
        {
            #region Contracts

            if (context == null)
            {
                throw new ArgumentNullException();
            }

            #endregion

            #region 產生人員權限管理物件(Manager)
            #region 使用者物件
            ApplicationUserManager userManager = ApplicationUserManager.Create(context);
            #endregion
            #region 角色物件
            ApplicationRoleManager roleManager = ApplicationRoleManager.Create(context);
            #endregion
            #region 讀取權限物件
            ApplicationPermissionManager permissionManager = ApplicationPermissionManager.Create(context);
            #endregion
            #endregion

            #region 初始化人員權限管理(Initialize)
            try
            {
                ReadJsonFile rjf = new ReadJsonFile();
                rjf.Action(JsonFile, out string[] UserName, out string[] UserPassword, out string[] Role, out string[] Permission);
                #region 預設的使用者(Default - User)
                string adminUserName     = UserName[0];
                string adminUserPassword = UserPassword[0];

                string guestUserName     = UserName[1];
                string guestUserPassword = UserPassword[1];
                #endregion
                #region 預設的角色(Default - Role)
                string adminRoleName = Role[0];
                string guestRoleName = Role[1];
                #endregion
                #region 預設的讀取權限(Default - Permission)
                string accessPermissionName        = Permission[0];
                string contactPermissionName       = Permission[1];
                string productAddPermissionName    = Permission[2];
                string productRemovePermissionName = Permission[3];
                #endregion
                #region 新增預設的使用者(Setup Default - User)
                var adminUser = userManager.FindByName(adminUserName);
                if (adminUser == null)
                {
                    adminUser = new ApplicationUser {
                        UserName = adminUserName, Email = adminUserName
                    };
                    userManager.Create(adminUser, adminUserPassword);
                    userManager.SetLockoutEnabled(adminUser.Id, false);
                }

                var guestUser = userManager.FindByName(guestUserName);
                if (guestUser == null)
                {
                    guestUser = new ApplicationUser {
                        UserName = guestUserName, Email = guestUserName
                    };
                    userManager.Create(guestUser, guestUserPassword);
                    userManager.SetLockoutEnabled(guestUser.Id, false);
                }
                #endregion
                #region 新增預設的角色(Setup Default - Role)
                var adminRole = roleManager.FindByName(adminRoleName);
                if (adminRole == null)
                {
                    adminRole = new ApplicationRole(adminRoleName);
                    roleManager.Create(adminRole);
                }

                var guestRole = roleManager.FindByName(guestRoleName);
                if (guestRole == null)
                {
                    guestRole = new ApplicationRole(guestRoleName);
                    roleManager.Create(guestRole);
                }
                #endregion
                #region 新增預設的讀取權限(Setup Default - Permission)
                var accessPermission = permissionManager.FindByName(accessPermissionName);
                if (accessPermission == null)
                {
                    accessPermission = new ApplicationPermission(accessPermissionName);
                    permissionManager.Create(accessPermission);
                }

                var contactPermission = permissionManager.FindByName(contactPermissionName);
                if (contactPermission == null)
                {
                    contactPermission = new ApplicationPermission(contactPermissionName);
                    permissionManager.Create(contactPermission);
                }

                var productAddPermission = permissionManager.FindByName(productAddPermissionName);
                if (productAddPermission == null)
                {
                    productAddPermission = new ApplicationPermission(productAddPermissionName);
                    permissionManager.Create(productAddPermission);
                }

                var productRemovePermission = permissionManager.FindByName(productRemovePermissionName);
                if (productRemovePermission == null)
                {
                    productRemovePermission = new ApplicationPermission(productRemovePermissionName);
                    permissionManager.Create(productRemovePermission);
                }
                #endregion
                #region 導入角色給預設的使用者(UserAddToRole)
                IList <string> rolesForUser = null;

                rolesForUser = userManager.GetRoles(adminUser.Id);
                if (rolesForUser.Contains(adminRole.Name) == false)
                {
                    userManager.AddToRole(adminUser.Id, adminRole.Name);
                }

                rolesForUser = userManager.GetRoles(guestUser.Id);
                if (rolesForUser.Contains(guestRole.Name) == false)
                {
                    userManager.AddToRole(guestUser.Id, guestRole.Name);
                }
                #endregion
                #region 導入讀取權限給角色(PermissionAddToRole)
                IList <string> rolesForPermission = null;

                rolesForPermission = permissionManager.GetRolesById(accessPermission.Id);
                if (rolesForPermission.Contains(adminRole.Name) == false)
                {
                    permissionManager.AddToRole(accessPermission.Id, adminRole.Name);
                }

                rolesForPermission = permissionManager.GetRolesById(contactPermission.Id);
                if (rolesForPermission.Contains(adminRole.Name) == false)
                {
                    permissionManager.AddToRole(contactPermission.Id, adminRole.Name);
                }

                rolesForPermission = permissionManager.GetRolesById(productAddPermission.Id);
                if (rolesForPermission.Contains(adminRole.Name) == false)
                {
                    permissionManager.AddToRole(productAddPermission.Id, adminRole.Name);
                }

                rolesForPermission = permissionManager.GetRolesById(productRemovePermission.Id);
                if (rolesForPermission.Contains(adminRole.Name) == false)
                {
                    permissionManager.AddToRole(productRemovePermission.Id, adminRole.Name);
                }
                #endregion
            }
            finally
            {
                // Dispose
                userManager.Dispose();
                roleManager.Dispose();
                permissionManager.Dispose();
            }
            #endregion
        }
예제 #2
0
 // Methods
 public static ApplicationPermissionManager Create(IdentityFactoryOptions <ApplicationPermissionManager> options, IOwinContext context)
 {
     return(ApplicationPermissionManager.Create(context.Get <ApplicationDbContext>()));
 }