Example #1
0
func main() {
	confpath := flag.String("config", "", "config path")

	flag.Parse()

	cfg, err := readeef.ReadConfig(*confpath)
	if err != nil {
		exitWithError(fmt.Sprintf("Error reading config from path '%s': %v", *confpath, err))
	}

	db := readeef.NewDB(cfg.DB.Driver, cfg.DB.Connect)
	if err := db.Connect(); err != nil {
		exitWithError(fmt.Sprintf("Error connecting to database: %v", err))
	}

	switch flag.Arg(0) {
	case "add":
		if flag.NArg() != 3 {
			exitWithError("Not enough arguments for 'add' command. Login and password must be specified")
		}
		login := flag.Arg(1)
		pass := flag.Arg(2)

		u := readeef.User{Login: login, Active: true}
		if err := u.SetPassword(pass); err != nil {
			exitWithError(fmt.Sprintf("Error setting password for user '%s': %v", login, err))
		}

		if err := db.UpdateUser(u); err != nil {
			exitWithError(fmt.Sprintf("Error updating the user database record for '%s': %v", login, err))
		}
	case "remove":
		if flag.NArg() != 2 {
			exitWithError("Not enough arguments for 'remove' command. Login must be specified")
		}
		login := flag.Arg(1)

		u, err := db.GetUser(login)
		if err != nil {
			exitWithError(fmt.Sprintf("Error getting user '%s' from the database: %v", login, err))
		}

		if err := db.DeleteUser(u); err != nil {
			exitWithError(fmt.Sprintf("Error removing user '%s' from the database: %v", login, err))
		}
	case "get":
		if flag.NArg() != 3 {
			exitWithError("Not enough arguments for 'get' command. Login and user property must be specified")
		}
		login := flag.Arg(1)
		prop := flag.Arg(2)

		u, err := db.GetUser(login)
		if err != nil {
			exitWithError(fmt.Sprintf("Error getting user '%s' from the database: %v", login, err))
		}

		lowerProp := strings.ToLower(prop)
		switch lowerProp {
		case "firstname", "first_name":
			fmt.Printf("%s\n", u.FirstName)
		case "lastname", "last_name":
			fmt.Printf("%s\n", u.LastName)
		case "email":
			fmt.Printf("%s\n", u.Email)
		case "hashtype", "hash_type":
			fmt.Printf("%s\n", u.HashType)
		case "salt":
			fmt.Printf("%v\n", u.Salt)
		case "hash":
			fmt.Printf("%v\n", u.Hash)
		case "md5api", "md5_api":
			fmt.Printf("%v\n", u.MD5API)
		case "admin":
			fmt.Printf("%v\n", u.Admin)
		case "active":
			fmt.Printf("%v\n", u.Active)
		default:
			exitWithError(fmt.Sprintf("Unknown user property '%s'", prop))
		}
	case "set":
		if flag.NArg() != 4 {
			exitWithError("Not enough arguments for 'update' command. Login, user property, and value must be specified")
		}
		login := flag.Arg(1)
		prop := flag.Arg(2)
		val := flag.Arg(3)

		u, err := db.GetUser(login)
		if err != nil {
			exitWithError(fmt.Sprintf("Error getting user '%s' from the database: %v", login, err))
		}

		lowerProp := strings.ToLower(prop)
		switch lowerProp {
		case "firstname", "first_name":
			u.FirstName = val
		case "lastname", "last_name":
			u.LastName = val
		case "email":
			u.Email = val
		case "password":
			if err := u.SetPassword(val); err != nil {
				exitWithError(fmt.Sprintf("Error setting password for user '%s': %v", u.Login, err))
			}
		case "admin", "active":
			enabled := false
			if val == "1" || val == "true" || val == "on" {
				enabled = true
			}
			if lowerProp == "admin" {
				u.Admin = enabled
			} else {
				u.Active = enabled
			}
		default:
			exitWithError(fmt.Sprintf("Unknown user property '%s'", prop))
		}

		if err := db.UpdateUser(u); err != nil {
			exitWithError(fmt.Sprintf("Error updating the user database record for '%s': %v", login, err))
		}
	case "list":
		users, err := db.GetUsers()
		if err != nil {
			exitWithError(fmt.Sprintf("Error getting users from the database: %v", err))
		}

		for _, u := range users {
			fmt.Printf("%s\n", u.Login)
		}
	case "list-detailed":
		users, err := db.GetUsers()
		if err != nil {
			exitWithError(fmt.Sprintf("Error getting users from the database: %v", err))
		}

		for _, u := range users {
			fmt.Printf("Login: %s", u.Login)
			if u.FirstName != "" {
				fmt.Printf(", first name: %s", u.FirstName)
			}
			if u.LastName != "" {
				fmt.Printf(", last name: %s", u.LastName)
			}
			if u.Email != "" {
				fmt.Printf(", email: %s", u.Email)
			}
			if u.HashType != "" {
				fmt.Printf(", has type: %s", u.HashType)
			}
			fmt.Printf("\n")
		}
	default:
		exitWithError(fmt.Sprintf("Unknown command '%s'", flag.Arg(0)))
	}
}
Example #2
0
func (con User) Handler(c context.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var err error

		db := readeef.GetDB(c)
		user := readeef.GetUser(c, r)

		if !user.Admin {
			readeef.Debug.Println("User " + user.Login + " is not an admin")

			w.WriteHeader(http.StatusForbidden)
			return
		}

		action := webfw.GetMultiPatternIdentifier(c, r)
		params := webfw.GetParams(c, r)
		resp := make(map[string]interface{})

		switch action {
		case "list":
			users, err := db.GetUsers()
			if err != nil {
				break
			}

			type user struct {
				Login     string
				FirstName string
				LastName  string
				Email     string
				Active    bool
				Admin     bool
			}

			userList := []user{}
			for _, u := range users {
				userList = append(userList, user{
					Login:     u.Login,
					FirstName: u.FirstName,
					LastName:  u.LastName,
					Email:     u.Email,
					Active:    u.Active,
					Admin:     u.Admin,
				})
			}

			resp["Users"] = userList
		case "add":
			login := params["login"]

			_, err = db.GetUser(login)
			/* TODO: non-fatal error */
			if err == nil {
				err = errors.New("User with login " + login + " already exists")
				break
			} else if err != sql.ErrNoRows {
				break
			}

			buf := util.BufferPool.GetBuffer()
			defer util.BufferPool.Put(buf)

			buf.ReadFrom(r.Body)

			u := readeef.User{Login: login}

			err = u.SetPassword(buf.String())
			if err != nil {
				break
			}

			err = db.UpdateUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		case "remove":
			login := params["login"]

			if user.Login == login {
				err = errors.New("The current user cannot be removed")
				break
			}

			var u readeef.User

			u, err = db.GetUser(login)
			if err != nil {
				break
			}

			err = db.DeleteUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		case "active":
			login := params["login"]

			if user.Login == login {
				err = errors.New("The current user cannot be removed")
				break
			}

			active := params["state"] == "true"

			var u readeef.User

			u, err = db.GetUser(login)
			if err != nil {
				break
			}

			u.Active = active
			err = db.UpdateUser(u)
			if err != nil {
				break
			}

			resp["Success"] = true
			resp["Login"] = login
		}

		var b []byte
		if err == nil {
			b, err = json.Marshal(resp)
		}
		if err != nil {
			webfw.GetLogger(c).Print(err)

			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		w.Write(b)
	}
}