예제 #1
0
        public ActionResult CreateNew(GroupModelDetail model)
        {
            if (!model.Roles.Any(n => n.IsCheck == true))
            {
                AlertWarning("Hãy chọn các quyền mà nhóm người dùng này được phép hoạt động.");
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                _groupService.BeginTran();
                try
                {
                    var check = _groupService.CheckContain(model.Group.Name);
                    if (check)
                    {
                        AlertWarning(InfoString.Instance.SetContainString("Tên nhóm"));
                        return(View(model));
                    }

                    var group = new Group();
                    group.Name        = model.Group.Name;
                    group.Descreption = model.Group.Descreption;
                    group.CreateBy    = CurrentInstance.Instance.CurrentUser.UserName;
                    group.CreateDate  = DateTime.Now;
                    group.Status      = model.Group.Status;
                    _groupService.CreateNew(group);
                    _groupService.CommitChanges();

                    //add roles to group
                    foreach (var role in model.Roles)
                    {
                        if (role.IsCheck)
                        {
                            _roleService.AddRoleToGroup(role.Id, group.Id);
                        }
                    }
                    _roleService.CommitChanges();
                    _groupService.CommitTran();
                }
                catch (Exception ex)
                {
                    LogError(ex);
                    _groupService.RollbackTran();
                    AlertError(InfoString.ERROR_SYSTEM);
                }
                AlertSuccess(InfoString.CREATE_SUCCESSFULL);
                return(RedirectToAction("Index"));
            }
            AlertWarning(InfoString.INVALID_INFO);
            return(View(model));
        }