Example #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
}
Example #2
0
//后台添加用户
func UserAdd(r render.Render, req *http.Request) {
	var err error
	data := make(map[string]interface{})
	if req.Method == "GET" {
		rs, err := dao.GetAllRole()
		if err != nil {
			service.Logs.Error("dao.GetAllUser() err(%v)", err)
			return
		}
		data["roleList"] = rs
		r.HTML(200, "user_add", data)
		return
	}
	req.ParseForm()
	values := req.Form
	m := &model.User{}
	m.Account = values.Get("account")
	m.Password = values.Get("password")
	m.Info = values.Get("info")
	m.Name = values.Get("name")
	if !checkNull([]string{m.Account, m.Password, m.Info, m.Name}...) {
		service.Logs.Error("args err")
		return
	}
	status, _ := strconv.Atoi(values.Get("status"))
	m.Status = status
	roleId, _ := strconv.Atoi(values.Get("role_id"))
	//添加用户

	userId, err := dao.AddUser(m)
	if err != nil {
		service.Logs.Error("dao.InsertUser err(%v)", err)
		return
	}
	//添加用户角色
	ur := &model.UserRole{}
	ur.RoleId = int64(roleId)
	ur.UserId = userId
	_, err = dao.AddUserRole(ur)
	if err != nil {
		service.Logs.Error("dao.AddUserRole err(%v)", err)
		return
	}

	r.Redirect("/", 302)
	return
}