Exemple #1
0
func EditUserRole(userId, roleId int64) (err error) {
	_, err = dao.GetUserById(userId)
	if err != nil {
		Logs.Error("dao getuserbudi err(%v)", err)
		return
	}
	_, err = dao.GetRoleById(roleId)
	if err != nil {
		Logs.Error("dao GetRoleById err(%v)", err)
		return
	}
	ur, err := dao.GetUserRoleByUserId(userId)
	if err != nil && err != orm.ErrNoRows {
		Logs.Error("dao GetUserRoleByUserId err(%v)", err)
		return
	}
	if err == orm.ErrNoRows {
		ur = &model.UserRole{}
		ur.UserId = userId
		ur.RoleId = roleId
		_, err = dao.AddUserRole(ur)
		if err != nil {
			Logs.Error("dao AddUserRole err(%v)", err)
			return
		}
		return
	}
	ur.RoleId = roleId
	err = dao.UpdateUserRoleById(ur)
	if err != nil {
		Logs.Error("dao UpdateUserRoleById err(%v)", err)
		return
	}
	return
}
Exemple #2
0
func EditUser(id int64, name, info string, status int, pass string) (err error) {
	m, err := dao.GetUserById(id)
	if err != nil {
		Logs.Error("dao.GetUserById err(%v)", err)
		return
	}
	m.Name = name
	m.Info = info
	m.Status = status
	err = dao.UpdateUserById(m)
	return
}
Exemple #3
0
//管理后台修改用户
func UserEdit(r render.Render, req *http.Request) {
	req.ParseForm()
	values := req.Form
	id := values.Get("id")
	var idInt64 int64
	var err error
	m := &model.User{}
	data := make(map[string]interface{})
	l, err := dao.GetAllRole()
	if err != nil {
		service.Logs.Error("dao.GetAllRole() err(%v)", err)
		return
	}
	data["l"] = l

	if id == "" {
		service.Logs.Error("id==null)")
		return
	}

	idInt64, err = strconv.ParseInt(id, 10, 64)
	if err != nil {
		service.Logs.Error("strconv.ParseInt err(%v)", err)
		return
	}

	rs, err := dao.GetAllRole()
	if err != nil {
		service.Logs.Error("dao.GetAllUser() err(%v)", err)
		return
	}
	data["roleList"] = rs

	if req.Method == "GET" {
		m, err = dao.GetUserById(idInt64)
		if err != nil {
			service.Logs.Error("dao.GetRoleById err(%v)", err)
			return
		}
		data["m"] = m
		r.HTML(200, "user_edit", data)
		return
	}
	m.Account = values.Get("account")
	m.Info = values.Get("info")
	m.Name = values.Get("name")
	m.Id = idInt64
	status, _ := strconv.Atoi(values.Get("status"))
	m.Status = status
	roleId := values.Get("role_id")
	roleIdInt, _ := strconv.Atoi(roleId)

	//更新用户角色
	err = service.EditUserRole(m.Id, int64(roleIdInt))
	if err != nil {
		service.Logs.Error("service.EditUserRole err(%v)", err)
		return
	}

	//更新用户
	err = dao.UpdateUserById(m)
	if err != nil {
		service.Logs.Error("dao.UpdateUserById err(%v)", err)
		return
	}
	data["m"] = m
	r.HTML(200, "user_edit", data)
	return
}