Esempio n. 1
0
 /// <summary>
 /// 角色分配
 /// </summary>
 /// <param name="listInfo">用户角色关系实体集合</param>
 /// <returns>分配结果</returns>
 public byte RoleAssign(RoleAssignViewModel listInfo)
 {
     using (DataSubmittedEntities db = new DataSubmittedEntities())
     {
         using (TransactionScope transaction = new TransactionScope())
         {
             //删除用户角色关系
             var list = db.OT_UserRole.Where(a => listInfo.UserIdList.Contains(a.UserId)).ToList();
             db.OT_UserRole.RemoveRange(list);
             //构建并添加用户角色关系
             foreach (Guid uesrId in listInfo.UserIdList)
             {
                 foreach (Guid roleId in listInfo.RoleIdList)
                 {
                     if (roleId == null)
                     {
                         return (byte)EResult.IsNull3;
                     }
                     OT_UserRole userRoleInfo = new OT_UserRole();
                     userRoleInfo.Id = Guid.NewGuid();
                     userRoleInfo.IsDelete = (byte)EDataStatus.Normal;
                     userRoleInfo.RoleId = roleId;
                     userRoleInfo.UserId = uesrId;
                     db.OT_UserRole.Add(userRoleInfo);
                 }
             }
             return Result.SaveChangesResult(db, transaction).ResultKey;
         }
     }
 }
        /// <summary>
        /// 角色分配
        /// </summary>
        /// <returns></returns>
        public JsonResult UserRoleAssign(RoleAssignViewModel roleAssignList)
        {
            if (roleAssignList == null || roleAssignList.UserIdList.Count < 1)
                return Json((byte)EResult.IsNull1, JsonRequestBehavior.DenyGet);

            if (roleAssignList != null && roleAssignList.RoleIdList.Count < 1)
                return Json((byte)EResult.IsNull2, JsonRequestBehavior.DenyGet);

            byte result = userManage.RoleAssign(roleAssignList);

            string user = string.Empty;
            foreach (Guid id in roleAssignList.UserIdList)
            {
                user = string.Format("{0}{1},", user, userManage.GetNamebyId(id));
            }

            string role = string.Empty;
            foreach (Guid id in roleAssignList.RoleIdList)
            {
                role = string.Format("{0}{1},", role, userManage.GetRoleNamebyId(id));
            }

            if (Session["UserInfo"] != null)
                ReportFactory.Instance.log.WriteLog(Common.OperationType.Update, string.Format("分配用户{0}角色为{1}", user, role));

            return Json(result, JsonRequestBehavior.DenyGet);
        }