public ActionResult Edit(Guid? id, SysEnterprise collection)
        {
            if (!ModelState.IsValid)
            {
                return View(collection);
            }

            _sysEnterpriseService.Save(id, collection);

            if (!id.HasValue)
            {
                //添加企业的时候需要给该企业初始化 角色和管理员账户

                //新建角色

                var sysRoleSysControllerSysActions = new List<SysRoleSysControllerSysAction>();

                foreach (var aa in _sysControllerSysActionService.GetAllEnt().Where(a => a.SysController.SysArea.AreaName == "Platform"))
                {
                    sysRoleSysControllerSysActions.Add(new SysRoleSysControllerSysAction
                    {
                        EnterpriseId = collection.Id,
                        SysControllerSysAction = aa
                    });
                }

                var role = new SysRole
                               {
                                   EnterpriseId = collection.Id,
                                   RoleName = "管理员",
                                   SysRoleSysControllerSysActions = sysRoleSysControllerSysActions,

                               };
                //新建管理员
                var user = new SysUser
                               {
                                   EnterpriseId = collection.Id,
                                   UserName = "******",
                                   Password = "******",
                                   OldPassword = "******",
                                   DisplayName = "管理员",
                                   SysRoleSysUsers = new List<SysRoleSysUser>
                                                       {
                                                           new SysRoleSysUser
                                                               {
                                                                   EnterpriseId = collection.Id,
                                                                   SysRole=role,
                                                               }
                                                       }
                               };

                _sysUserService.Add(user);

            }

            _unitOfWork.Commit();

            return RedirectToAction("Index");

        }
        //
        // GET: /Platform/SysEnterprise/Edit/5

        public ActionResult Edit(Guid? id)
        {
            var item = new SysEnterprise();
            if (id.HasValue)
            {
                item = _sysEnterpriseService.GetById(id.Value);
            }
            return View(item);
        }
예제 #3
0
        public ActionResult Index(InitializeModel item)
        {
            if (applicationDbContext.SysEnterprises.Any(u => !u.Deleted && u.EnterpriseName == item.EnterpriseName))
            {
                ModelState.AddModelError("EnterpriseName", @"该企业名称已存在");
            }

            if (applicationDbContext.SysEnterprises.Any(u => !u.Deleted && u.EnterpriseShortName == item.EnterpriseShortName))
            {
                ModelState.AddModelError("EnterpriseShortName", @"该企业简称已存在");
            }

            if (!ModelState.IsValid)
            {
                return View(item);
            }

            try
            {
                #region SysEnterprise

                var host = Spell.MakeSpellCode(item.EnterpriseShortName, SpellOptions.FirstLetterOnly);

                //检查Host是否已经被占用

                while (applicationDbContext.SysEnterprises.Any(a => a.Host == host))
                {
                    host = host + new Random().Next(11, 99);
                }

                host = host + ".wjw1.com";

                var sysEnterprise = new SysEnterprise { EnterpriseName = item.EnterpriseName, EnterpriseShortName = item.EnterpriseShortName, Host = host };

                applicationDbContext.SysEnterprises.Add(sysEnterprise);

                #endregion

                //添加管理员权限栏位
                #region SysRole

                //var sysRole = new SysRole { RoleName = "管理员", EnterpriseId = sysEnterprise.Id };
                var sysRole = new SysRole() { RoleName = "管理员" };

                applicationDbContext.SysRoles.Add(sysRole);

                #endregion

                //添加管理员账号
                #region Add Administrator User

                var sysUser = new SysUser
                {
                    //EnterpriseId = sysEnterprise.Id,
                    UserName = item.UserName,
                    DisplayName = "系统管理员",
                    Email = item.Email,
                    Password = HashPassword.GetHashPassword(item.Password),
                    OldPassword = HashPassword.GetHashPassword(item.Password),
                };

                applicationDbContext.SysUsers.Add(sysUser);

                #endregion

                //对应管理员账号和管理员权限
                #region SysRoleSysUser

                var sysRoleSysUser = new SysRoleSysUser
                {
                    //EnterpriseId = sysEnterprise.Id,
                    SysUserId = sysUser.Id,
                    SysRoleId = sysRole.Id
                };

                applicationDbContext.SysRoleSysUsers.Add(sysRoleSysUser);

                #endregion

                //添加详细权限给予管理员角色
                #region SysRoleSysControllerSysAction

                var sysRoleSysControllerSysAction = new List<SysRoleSysControllerSysAction>();

                foreach (var sysControllerSysAction in applicationDbContext.SysControllerSysActions)
                {
                    sysRoleSysControllerSysAction.Add(new SysRoleSysControllerSysAction
                    {
                        //EnterpriseId = sysEnterprise.Id,
                        SysControllerSysActionId = sysControllerSysAction.Id,
                        SysRoleId = sysRole.Id
                    });
                }

                sysRoleSysControllerSysAction.ForEach(a => applicationDbContext.SysRoleSysControllerSysActions.Add(a));

                #endregion

                applicationDbContext.SaveChanges();

                TempData[Alerts.SUCCESS] = "注册成功!";

                return RedirectToAction("Index", "Succes", new { Id = sysEnterprise.Id });
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", e.Message);
                return View(item);
            }
        }