Beispiel #1
0
func ListUsers(db *bolt.DB) {
	users, err := quimby.GetUsers(db)
	if err != nil {
		log.Fatal(err)
	}
	listUsers(users)
}
Beispiel #2
0
func GetUsers(w http.ResponseWriter, req *http.Request) {
	args := GetArgs(req)
	users, err := quimby.GetUsers(args.DB)
	if err != nil {
		context.Set(req, "error", err)
		return // err
	}

	enc := json.NewEncoder(w)
	enc.Encode(users)
}
Beispiel #3
0
func AdminPage(w http.ResponseWriter, req *http.Request) {
	args := handlers.GetArgs(req)
	gadgets, err := quimby.GetGadgets(args.DB)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	links := make([]link, len(gadgets))
	for i, g := range gadgets {
		links[i] = link{Name: g.Name, Path: fmt.Sprintf("/admin/gadgets/%s", g.Id)}
	}

	users, err := quimby.GetUsers(args.DB)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	userLinks := make([]link, len(users))
	for i, u := range users {
		userLinks[i] = link{Name: u.Username, Path: fmt.Sprintf("/admin/users/%s", u.Username)}
	}

	i := adminPage{
		userPage: userPage{
			User:  args.User.Username,
			Admin: handlers.Admin(args),
			Links: []link{
				{"quimby", "/"},
				{"admin", "/admin.html"},
			},
		},
		Gadgets: links,
		Users:   userLinks,
	}
	templates["admin.html"].template.ExecuteTemplate(w, "base", i)
}
Beispiel #4
0
func EditUser(db *bolt.DB) {
	users, err := quimby.GetUsers(db)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("select a user")
	listUsers(users)

	var i int
	fmt.Scanf("%d\n", &i)
	u := users[i-1]

	var d string
	fmt.Printf("Delete user %s?  (y/N)\n ", u.Username)
	fmt.Scanf("%s\n", &d)
	if d == "y" {
		u.Delete()
		return
	}

	var p int
	fmt.Printf("permission (%s):\n  1: read\n  2: write\n  3: admin\n ", u.Permission)
	fmt.Scanf("%d\n", &p)
	perm, ok := permissions[p]
	if ok {
		u.Permission = perm
	}

	var c string
	fmt.Print("change tfa? (y/N) ")
	fmt.Scanf("%s\n", &c)
	if c == "y" || c == "Y" {
		if os.Getenv("QUIMBY_DOMAIN") == "" {
			log.Fatal("you must set QUIMBY_DOMAIN")
		}
		tfa := quimby.NewTFA(os.Getenv("QUIMBY_DOMAIN"))
		if err := u.Fetch(); err != nil {
			log.Fatal(err)
		}

		u.SetTFA(tfa)

		qr, err := u.UpdateTFA()
		if err != nil {
			log.Fatal(err)
		}

		tmp, err := ioutil.TempFile("", "")
		if err != nil {
			log.Fatal(err)
		}

		if _, err := tmp.Write(qr); err != nil {
			log.Fatal(err)
		}
		tmp.Close()
		fmt.Printf("you must scan the qr at %s with google authenticator before you can log in\n", tmp.Name())
	}

	c = ""
	fmt.Print("change password? (y/N) ")
	fmt.Scanf("%s\n", &c)
	if c == "y" || c == "Y" {
		getPasswd(&u)
	}

	log.Println(u.Save())
}