Ejemplo n.º 1
0
func CreateAdmin(r render.Render, req *http.Request) {
	if len(cache.Users) > 0 {
		r.Error(404)
		return
	}
	if req.Method == "GET" {
		r.HTML(200, "user_add", map[string]interface{}{})
		return
	}

	req.ParseForm()
	values := req.Form
	m := &model.User{}
	m.Account = values.Get("account")
	m.Password = model.Md5(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
	}
	m.Status = model.UserStatusAdmin
	_, err := dao.AddUser(m)
	if err != nil {
		service.Logs.Error("dao.InsertUser err(%v)", err)
		return
	}
	r.Redirect("/", 302)
	return
}
Ejemplo n.º 2
0
func AddUser(id int64, name, info, pass string) (err error) {
	m := &model.User{}
	m.Id = id
	m.Name = name
	m.Info = info
	m.Ctime = time.Now().Unix()
	_, err = dao.AddUser(m)
	return
}
Ejemplo n.º 3
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
}