Beispiel #1
0
        public ActionResult AddRole(string checkedItems, string roleName)
        {
            try
            {
                var rolePermissions = new List<System_Role_Permission>();
                if (string.IsNullOrEmpty(checkedItems))
                {
                    rolePermissions = null;
                }
                else
                {
                    checkedItems = checkedItems.Substring(1, checkedItems.Length - 1);

                    var permissionIDs = checkedItems.Split(',');
                    foreach (var permissionID in permissionIDs)
                    {
                        rolePermissions.Add(new System_Role_Permission { PermissionID = Convert.ToInt32(permissionID) });
                    }
                }

                this.systemRoleService = new SystemRoleService();

                var systemRole = new System_Role { Name = roleName, Headcount = 0, CreateTime = DateTime.Now };
                systemRole.ID = this.systemRoleService.AddRole(systemRole, rolePermissions);

                return this.Content("1");
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message, exception);
            }
        }
        /// <summary>
        /// 添加系统角色
        /// </summary>
        /// <param name="role">
        /// 角色对象
        /// </param>
        /// <param name="rolePermissions">
        /// 角色权限关系对象列表
        /// </param>
        /// <returns>
        /// 角色编号
        /// </returns>
        /// <exception cref="Exception">
        /// 执行添加系统角色操作异常
        /// </exception>
        public int AddRole(System_Role role, List<System_Role_Permission> rolePermissions)
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }

            if (rolePermissions == null)
            {
                throw new ArgumentNullException("rolePermissions");
            }

            if (rolePermissions.Count <= 0)
            {
                return -1;
            }

            try
            {
                SqlTransaction transaction;
                var roleID = this.systemRoleDA.Insert(role, out transaction);

                foreach (var systemRolePermission in rolePermissions)
                {
                    systemRolePermission.RoleID = roleID;
                    this.systemRolePermissionDA.Insert(systemRolePermission, transaction);
                }

                transaction.Commit();
                return roleID;
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message, exception);
            }
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role">
        /// 角色对象
        /// </param>
        /// <param name="transaction">
        /// 事务对象
        /// </param>
        /// <returns>
        /// 主键编号
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// 参数为空异常
        /// </exception>
        /// <exception cref="Exception">
        /// 数据库操作异常
        /// </exception>
        public int Insert(System_Role role, out SqlTransaction transaction)
        {
            if (role == null)
            {
                throw new ArgumentNullException("role");
            }

            int id;
            var parameters = new List<SqlParameter>
                                 {
                                     this.SqlServer.CreateSqlParameter(
                                         "Name",
                                         SqlDbType.NVarChar,
                                         role.Name,
                                         ParameterDirection.Input),
                                     this.SqlServer.CreateSqlParameter(
                                         "Headcount",
                                         SqlDbType.Int,
                                         role.Headcount,
                                         ParameterDirection.Input),
                                     this.SqlServer.CreateSqlParameter(
                                         "CreateTime",
                                         SqlDbType.DateTime,
                                         role.CreateTime,
                                         ParameterDirection.Input),
                                     this.SqlServer.CreateSqlParameter(
                                         "ReferenceID",
                                         SqlDbType.Int,
                                         null,
                                         ParameterDirection.Output)
                                 };

            try
            {
                this.SqlServer.BeginTransaction();
                transaction = this.SqlServer.Transaction;

                this.SqlServer.ExecuteNonQuery(CommandType.StoredProcedure, "sp_System_Role_Insert", parameters, null);
                id = (int)parameters.Find(parameter => parameter.ParameterName == "ReferenceID").Value;
            }
            catch (Exception exception)
            {
                this.SqlServer.RollbackTransaction();
                throw new Exception(exception.Message, exception);
            }

            return id;
        }