Example #1
0
func RoleAdd(r render.Render, req *http.Request) {
	req.ParseForm()
	values := req.Form
	id := values.Get("id")
	var idInt64 int64
	var err error
	m := &model.Role{}
	data := make(map[string]interface{})

	l, err := dao.GetAllPermission()
	if err != nil {
		service.Logs.Error("dao.GetAllPermission() err(%v)", err)
	}
	data["l"] = l

	list := dao.GetFormatPermission()
	data["list"] = list

	if id != "" {
		idInt64, err = strconv.ParseInt(id, 10, 64)
		if err != nil {
			service.Logs.Error("strconv.ParseInt err(%v)", err)
			return
		}
	}
	if req.Method == "GET" {
		if id != "" && idInt64 != 0 {
			m, err = dao.GetRoleById(idInt64)
			if err != nil {
				service.Logs.Error("dao.GetRoleById err(%v)", err)
				return
			}
		}
		p := cache.GetRolePermissions(idInt64)
		data["p"] = p
		data["m"] = m
		r.HTML(200, "role_add", data)
		return
	}
	m.Info = values.Get("info")
	m.Name = values.Get("name")
	if id == "" || idInt64 == 0 {
		m.Status = 0
		mId, err := dao.AddRole(m)
		if err != nil {
			service.Logs.Error("dao.AddRole err(%v)", err)
			return
		}
		m, _ = dao.GetRoleById(mId)
	} else {
		m.Id = idInt64
		status, _ := strconv.Atoi(values.Get("status"))
		m.Status = status
		err = dao.UpdateRoleById(m)
		if err != nil {
			service.Logs.Error("dao.UpdateRoleById err(%v)", err)
			return
		}
	}
	p := cache.GetRolePermissions(idInt64)

	data["p"] = p
	data["m"] = m

	r.HTML(200, "role_add", data)
	return
}
Example #2
0
//管理后台 加减权限
func RolePermissionAdd(r render.Render, req *http.Request) {
	req.ParseForm()
	values := req.Form
	var err error
	//角色id
	roleId := values.Get("role_id")
	roleIdInt, err := strconv.ParseInt(roleId, 10, 64)
	if err != nil {
		service.Logs.Error("strconv.ParseInt err(%v)", err)
		return
	}
	//权限列表
	permissionsAddStr := values.Get("permission_add_list")
	service.Logs.Debug("add list %s", permissionsAddStr)

	existIds := cache.GetRolePermissions(roleIdInt)

	addList := make(map[int64]int64)
	for _, v := range strings.Split(permissionsAddStr, ",") {
		if v == "" {
			continue
		}
		idInt, err := strconv.ParseInt(v, 10, 64)
		if err == nil {
			addList[idInt] = idInt
		}
	}

	if len(existIds) > 0 {
		for _, n := range existIds {
			if _, ok := addList[n.Id]; ok {
				delete(addList, n.Id)

			}
		}
	}

	for _, v := range addList {
		m := &model.RolePermission{}
		m.RoleId = roleIdInt
		m.PermissionId = v
		_, err = dao.AddRolePermission(m)
		if err != nil {
			service.Logs.Error("dao.AddRolePermission err(%v)", err)
			continue
		}
	}
	permissionsDelStr := values.Get("permission_del_list")
	service.Logs.Debug("del list %s", permissionsDelStr)

	delList := make(map[int64]int64)
	for _, v := range strings.Split(permissionsDelStr, ",") {
		if v == "" {
			continue
		}
		for _, e := range existIds {
			if v == strconv.FormatInt(e.Id, 10) {
				if _, ok := delList[e.Id]; !ok {
					delList[e.Id] = e.Id
				} else {
					fmt.Println("Key IsFound")
				}
			}
		}
	}

	for _, v := range delList {
		err = dao.DeleteRoleIdAndPermissionId(roleIdInt, v)
		if err != nil {
			service.Logs.Error("dao.DelRolePermission err(%v)", err)
			continue
		}
	}
	service.Logs.Debug("redirect /role/add?id=%d", roleIdInt)
	r.Redirect(fmt.Sprintf("/role/add?id=%d", roleIdInt), 302)
	return
}