zoukankan      html  css  js  c++  java
  • Weapsy 分析三(Web层) 2 初始化

    Weapsy的初始化比较复杂,接口和实现类使用Ninject进行映射。Model和Entity使用AtuotMapper进行转换。

    还有各种安装服务,所以单独看了下。

    相关代码:

    Weapsy\Web\Framework\InjectionModule.cs           --执行注入

    Weapsy\Web\Models\AutoMapperStartupTask.cs   --执行映射

    F:\外面的项目\Weapsy\Libraries\Services\InstallService.cs  --安装服务

    下面是我对InstallService.cs中最重要的Initialize()方法的注释。

    	public void Initialize()
            {
                // ==============================
                //  USERS AND ROLES  分超级管理员和管理员 带Email
                // ==============================
    
                string superAdminName = AppSettings.UserSuperAdminName;
                string superAdminEmail = AppSettings.UserSuperAdminEmail;
                string adminName = AppSettings.UserAdminName;
                string adminEmail = AppSettings.UserAdminEmail;
                string roleSuperAdmin = AppSettings.RoleSuperAdmin;
                string roleAdmin = AppSettings.RoleAdmin;
    
                string superAdminMembershipName = "";
                string adminMembershipName = "";
    
                //默认的关系  使用Email做帐号 还是用户名  默认是Email做帐号
                if (AppSettings.DefaultMembershipName == "Email")
                {
                    superAdminMembershipName = AppSettings.UserSuperAdminEmail;
                    adminMembershipName = AppSettings.UserAdminEmail;
                }
                else
                {
                    superAdminMembershipName = AppSettings.UserSuperAdminName;
                    adminMembershipName = AppSettings.UserAdminName;
                }
    
                //如果不存在超级管理员 就创建一个超级管理员
                if (Membership.GetUser(superAdminMembershipName) == null)
                {
                    Membership.CreateUser(superAdminMembershipName, "123456", superAdminEmail);
                }
    
                //如果不存在用户管理员 就创建一个用户管理员
                if (Membership.GetUser(adminMembershipName) == null)
                {
                    Membership.CreateUser(adminMembershipName, "123456", adminEmail);
                }
    
                //如果不存在超级管理员的角色权限 创建个
                if (!Roles.RoleExists(roleSuperAdmin))
                {
                    Roles.CreateRole(roleSuperAdmin);
                }
    
                //如果不存在管理员的角色权限 创建个
                if (!Roles.RoleExists(roleAdmin))
                {
                    Roles.CreateRole(roleAdmin);
                }
    
                //如果超级管理员的角色权限没有roleSuperAdmin,添加这个帐号到超级管理员中
                if (!Roles.IsUserInRole(superAdminMembershipName, roleSuperAdmin))
                {
                    Roles.AddUserToRole(superAdminMembershipName, roleSuperAdmin);
                }
    
                //如果用户管理员的角色权限没有roleAdmin,添加这个帐号到用户管理员中
                if (!Roles.IsUserInRole(adminMembershipName, roleAdmin))
                {
                    Roles.AddUserToRole(adminMembershipName, roleAdmin);
                }
    
                //获取超级管理员帐号
                MembershipUser mSuperAdmin = Membership.GetUser(superAdminMembershipName);
    
                //不通过缓存获取主数据库的超级管理员帐号信息 如果不存在创建   这里不控制权限 仅仅是帐号
                if (_userService.GetByMembershipName(mSuperAdmin.UserName, false) == null)
                {
                    User SuperAdmin = new User();
    
                    SuperAdmin.MembershipName = mSuperAdmin.UserName;
                    SuperAdmin.MembershipEmail = mSuperAdmin.Email;
                    SuperAdmin.MembershipDate = mSuperAdmin.CreationDate;
    
                    SuperAdmin.DisplayName = superAdminName;
                    SuperAdmin.ProfileName = superAdminName;
    
                    SuperAdmin.CreatedByUserId = Consts.NullId;
                    SuperAdmin.CreatedByIpAddress = HttpContext.Current.Request.UserHostAddress;
                    SuperAdmin.CreatedOnDate = Utils.GetDateForDB();
    
                    SuperAdmin.ModifiedByUserId = Consts.NullId;
                    SuperAdmin.ModifiedByIpAddress = HttpContext.Current.Request.UserHostAddress;
                    SuperAdmin.ModifiedOnDate = Utils.GetDateForDB();
    
                    _userService.Create(SuperAdmin);
                }
    
                MembershipUser mAdmin = Membership.GetUser(adminMembershipName);
    
                ////不通过缓存获取主数据库的管理员帐号信息 如果不存在创建  这里不控制权限 仅仅是帐号
                if (_userService.GetByMembershipName(mAdmin.UserName, false) == null)
                {
                    User Admin = new User();
    
                    Admin.MembershipName = mAdmin.UserName;
                    Admin.MembershipEmail = mAdmin.Email;
                    Admin.MembershipDate = mAdmin.CreationDate;
    
                    Admin.DisplayName = adminName;
                    Admin.ProfileName = adminName;
    
                    Admin.CreatedByUserId = Consts.NullId;
                    Admin.CreatedByIpAddress = HttpContext.Current.Request.UserHostAddress;
                    Admin.CreatedOnDate = Utils.GetDateForDB();
    
                    Admin.ModifiedByUserId = Consts.NullId;
                    Admin.ModifiedByIpAddress = HttpContext.Current.Request.UserHostAddress;
                    Admin.ModifiedOnDate = Utils.GetDateForDB();
    
                    _userService.Create(Admin);
                }
    
                // ==============================
                //  SETTINGS
                // ==============================
    
                InstallSettings();
    
                // ==============================
                //  MODULE DEFINITIONS 模型定义 作用:
                // ==============================
    
                ModuleDefinition TextModuleDef = InstallModuleDefinition("Text", //模块名称
                    "Module for Html content",//介绍
                    "Text", //标题
                    "Text", //控制器名字
                    "Module", //方法
                    "Text", //管理员控制器
                    "Edit");//管理员方法 
    
                // ==============================
                //  PLUGIN DEFINITIONS   插件定义
                // ==============================
    
                PluginDefinition EditProfilePluginDef = InstallPluginDefinition(
                    PluginType.Account, //插件类型:帐号
                    "EditProfile", //插件名字
                    "Edit Profile", //插件描述
                    "Edit Profile", //菜单文本
                    "Edit Profile", //菜单标题
                    "Account", //控制器名称
                    "EditProfile", //方法名称
                    "", //管理员控制器名称
                    "", //管理员方法
                    "editprofile-dashboard.png", //仪表图标
                    "editprofile-menu.png");//菜单图标
                PluginDefinition ChangePasswordPluginDef = InstallPluginDefinition(
                    PluginType.Account, 
                    "ChangePassword", 
                    "Change Password", 
                    "Change Password", 
                    "Change Password", 
                    "Account", 
                    "ChangePassword", 
                    "", 
                    "", 
                    "changepassword-dashboard.png", 
                    "changepassword-menu.png");
                PluginDefinition ChangeEmailPluginDef = InstallPluginDefinition(
                    PluginType.Account, 
                    "ChangeEmail", 
                    "Change Email", 
                    "Change Email", 
                    "Change Email", 
                    "Account", 
                    "ChangeEmail", 
                    "", 
                    "", 
                    "changeemail-dashboard.png", 
                    "changeemail-menu.png");
    
                // ==============================
                //  WIDGET DEFINITIONS
                // ==============================
    
                // -----
    
                // ==============================
                //  THEMES  主题设置  默认主题:2012-黑色风格 排列第一
                // ==============================
    
                //
                Theme defaultTheme = InstallTheme("TwentyTwelve - Dark", 1);
    
                // ==============================
                //  PAGE TEMPLATES   页面模板
                // ==============================
    
                PageTemplate PageTemplate1 = InstallPageTemplate(
                    "Header-Left-Content-Right-Footer", //模板名称-
                    "Header,Left,Content,Right,Footer");//模板区域
                PageTemplate PageTemplate2 = InstallPageTemplate("Header-Content-Right-Footer", "Header,Content,Right,Footer");
                PageTemplate PageTemplate3 = InstallPageTemplate("Header-Left-Content-Footer", "Header,Left,Content,Footer");
                PageTemplate PageTemplate4 = InstallPageTemplate("Header-Left-Right-Footer", "Header,Left,Right,Footer");
                PageTemplate PageTemplate5 = InstallPageTemplate("Header-Content-Footer", "Header,Content,Footer");
    
                // ==============================
                //  MODULE TEMPLATES  模块/组件模板
                // ==============================
    
                ModuleTemplate ModuleTemplate1 = InstallModuleTemplate("Standard");//标准模板
                ModuleTemplate ModuleTemplate2 = InstallModuleTemplate("Empty");//空模板
    
                // ==============================
                //  SITE   站点定义    安装默认的主题 第一个模板 第一个模块
                // ==============================
    
                Site site = InstallSite(defaultTheme.Id, PageTemplate1.Id, ModuleTemplate1.Id);
    
                // ==============================
                //  LANGUAGES  语言定义
                // ==============================
    
                List<Language> Languages = new List<Language>();
    
                //安装默认语言    默认语言是en-US               
                Language defaultLanguage = InstallLanguage(site.Id, AppSettings.DefaultLanguage, 1);
    
                Languages.Add(defaultLanguage);
    
                // ==============================
                //  MAIL TEMPLATE TYPES   邮件模板类型  
                // ==============================
    
                //注册邮箱模板类型
                MailTemplateType registerMailTemplateType = InstallMailTemplateType(new MailTemplateType()
                {
                    AppId = Consts.NullId,
                    Name = "Register",
                    Description = "Register",
                    TokenController = "MailTemplateType",
                    TokenAction = "RegisterTokens"
                });
    
                //密码恢复邮箱模板
                MailTemplateType passwordRecoveryMailTemplateType = InstallMailTemplateType(new MailTemplateType()
                {
                    AppId = Consts.NullId,
                    Name = "PasswordRecovery",
                    Description = "Password Recovery",
                    TokenController = "MailTemplateType",
                    TokenAction = "PasswordRecoveryTokens"
                });
    
                //改密码邮箱模板
                MailTemplateType changePasswordMailTemplateType = InstallMailTemplateType(new MailTemplateType()
                {
                    AppId = Consts.NullId,
                    Name = "ChangePassword",
                    Description = "Change Password",
                    TokenController = "MailTemplateType",
                    TokenAction = "ChangePasswordTokens"
                });
    
                // ==============================
                //  MAIL TEMPLATES  邮箱模板
                // ==============================
    
                MailTemplate registerMailTemplate = InstallMailTemplate(new MailTemplate() 
                {
                    MailTemplateTypeId = registerMailTemplateType.Id,
                    SiteId = site.Id,
                    Name = "Register",
                    Description = "Register Default Template",
                    Subject = "Welcome to {SiteTitle}!",
                    BodyHtml = "<strong>...Body Html...</strong>",
                    BodyPlain = "...Body Plain...",
                });
    
                MailTemplate passwordRecoveryMailTemplate = InstallMailTemplate(new MailTemplate()
                {
                    MailTemplateTypeId = passwordRecoveryMailTemplateType.Id,
                    SiteId = site.Id,
                    Name = "PasswordRecovery",
                    Description = "Password Recovery Default Template",
                    Subject = "{SiteTitle} - Password Recovery",
                    BodyHtml = "<strong>...Your Password...</strong>",
                    BodyPlain = "...Your Password...",
                });
    
                MailTemplate changePasswordMailTemplate = InstallMailTemplate(new MailTemplate()
                {
                    MailTemplateTypeId = changePasswordMailTemplateType.Id,
                    SiteId = site.Id,
                    Name = "ChangePassword",
                    Description = "Change Password Default Template",
                    Subject = "{SiteTitle} - Change Password",
                    BodyHtml = "<strong>...Your Password...</strong>",
                    BodyPlain = "...Your Password...",
                });
    
                // ==============================
                //  EMAIL ACCOUNTS  邮箱帐号
                // ==============================
    
                EmailAccount defaultEmailAccount = InstallEmailAccount(new EmailAccount()
                {
                    SiteId = site.Id,
                    Address = "info@mail.com",
                    DisplayName = site.Title,
                    Host = "smtp.mail.com",
                    Port = 25,
                    UserName = "",
                    Password = "",
                    DefaultCredentials = false,
                    Ssl = false
                });
    
                // ==============================
                //  PAGES
                // ==============================
                //页面  安装了一个主页 
                Page Page1 = InstallPage(site.Id, "Home", "Welcome!"); 
    
                // ==============================
                //  MODULES
                // ==============================
    
                InstallTextModule(site.Id, Page1.Id, TextModuleDef.Id, "Left", 1, "Left Zone - Module 1", "First Text Module in Left Zone");
                InstallTextModule(site.Id, Page1.Id, TextModuleDef.Id, "Left", 2, "Left Zone - Module 2", "Second Text Module in Left Zone");
                InstallTextModule(site.Id, Page1.Id, TextModuleDef.Id, "Content", 1, "Content Zone - Module 1", "Weapsy " + Consts.Version + "<br /><br /><strong>Default Credentials</strong><br />login name: " + superAdminMembershipName +" <br />password: 123456");
                InstallTextModule(site.Id, Page1.Id, TextModuleDef.Id, "Content", 2, "Content Zone - Module 2", "Second Text Module in Content Zone");
                InstallTextModule(site.Id, Page1.Id, TextModuleDef.Id, "Right", 1, "Right Zone - Module 1", "First Text Module in Right Zone");
                InstallTextModule(site.Id, Page1.Id, TextModuleDef.Id, "Right", 2,  "Right Zone - Module 2","Second Text Module in Right Zone");
    
                // ==============================
                //  ACCOUNT PLUGINS
                // ==============================
    
                Plugin editProfilePlugin = InstallPlugin(site.Id, EditProfilePluginDef.Id, "Edit Profile", "Edit Profile", 1);
                Plugin changePasswordPlugin = InstallPlugin(site.Id, ChangePasswordPluginDef.Id, "Change Password", "Change Password", 2);
                Plugin changeEmailPlugin = InstallPlugin(site.Id, ChangeEmailPluginDef.Id, "Change Email", "Change Email", 3);
    
                // ==============================
                //  UPDATE SITE  更新站点信息: 语言 主页 注册邮箱模板 密码恢复模板改密码模板 发送者邮箱帐号 
                // ==============================
    
                Site siteToUpdate = new Site();
    
                siteToUpdate = _siteService.GetById(site.Id);
    
                siteToUpdate.LanguageId = defaultLanguage.Id;
                siteToUpdate.HomePageId = Page1.Id;
    
                siteToUpdate.RegisterMailTemplateId = registerMailTemplate.Id;
                siteToUpdate.PasswordRecoveryMailTemplateId = passwordRecoveryMailTemplate.Id;
                siteToUpdate.ChangePasswordMailTemplateId = changePasswordMailTemplate.Id;
    
                siteToUpdate.RegisterEmailAccountId = defaultEmailAccount.Id;
                siteToUpdate.PasswordRecoveryEmailAccountId = defaultEmailAccount.Id;
                siteToUpdate.ChangePasswordEmailAccountId = defaultEmailAccount.Id;
    
                _siteService.Update(siteToUpdate, new List<LocalizedSite>());
    
                // ==============================
                //  RESTART APPLICATION
                // ==============================
    
                Utils.RestartApplication();
    
                // ==============================
                //  REDIRECT TO HOME PAGE
                // ==============================
    
                HttpContext.Current.Response.Redirect("");
            }

    最后 Web层除控制器外,已经没多少好东西了。唯一要看的就是里面的Framework文件夹。 业务的东西大家按需阅读,里面都是一些

    机制的实现。

  • 相关阅读:
    php入门到精通(复习笔记)
    MySQL的基础(优化)3
    php之Apache压力测试
    DOS下常用命令
    php连接MySQL分析
    php-memcached详解
    php 四种基本排序算法
    MySQL的基础(优化)2
    SQL入门之集合操作
    SQL入门之多表查询
  • 原文地址:https://www.cnblogs.com/fengxing/p/2508820.html
Copyright © 2011-2022 走看看