// Methods
 public static ApplicationPermissionManager Create(IdentityFactoryOptions <ApplicationPermissionManager> options, IOwinContext context)
 {
     return(ApplicationPermissionManager.Create(context.Get <ApplicationDbContext>()));
 }
        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
            {
                #region 預設的使用者(Default - User)
                const string adminUserName     = "******";
                const string adminUserPassword = "******";

                const string guestUserName     = "******";
                const string guestUserPassword = "******";
                #endregion
                #region 預設的角色(Default - Role)
                const string adminRoleName = "Admin";
                const string guestRoleName = "Guest";
                #endregion
                #region 預設的讀取權限(Default - Permission)
                const string accessPermissionName        = "AccessAccess";
                const string contactPermissionName       = "ContactAccess";
                const string productAddPermissionName    = "ProductAddAccess";
                const string productRemovePermissionName = "ProductRemoveAccess";
                #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
        }
 // Constructors
 public ApplicationPermissionAuthorize(ApplicationPermissionManager permissionManager) : base(permissionManager)
 {
 }