Ejemplo n.º 1
0
/**
 * Handles saving a user.
 */
func (t *User) save(response http.ResponseWriter, request *http.Request) {
	// Try and load a user with the Username.
	existingUsername := request.FormValue("idUsername")
	userDataStoreHelper, err := datastore.GetUserHelper()
	if err != nil {
		log.Println("DB Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	myuser := datastore.User{}

	if existingUsername != "" {
		myuser, err = userDataStoreHelper.GetUser(existingUsername)

		// Check for error when loading user.
		if err != nil {
			http.Error(response, err.Error(), 500)
			return
		}
	}

	// Set the values on the object.
	myuser.Username = request.FormValue("username")
	myuser.DisplayName = request.FormValue("name")
	myuser.SetShortDOB(request.FormValue("dob"))

	if request.FormValue("isAdmin") == "true" {
		myuser.IsAdmin = true
	} else {
		myuser.IsAdmin = false
	}

	err = userDataStoreHelper.SetUser(&myuser)

	// Check for error when saving user.
	if err != nil {
		// There was an error, so report that to the screen.
		http.Redirect(response, request, t.Base()+"?saveerror=true", http.StatusMovedPermanently)
	} else {
		// No error, so report a successful save to the screen and remove the previous version
		// if username has been edited.

		// If the new username is different to the existing username, delete the old
		// version before saving the new version. This prevents duplicates.
		if existingUsername != "" && myuser.Username != existingUsername {
			err = userDataStoreHelper.DeleteUser(existingUsername)

		}
		http.Redirect(response, request, t.Base()+"?savecomplete=true", http.StatusMovedPermanently)
	}
}
Ejemplo n.º 2
0
/*
 * Create a User in the Database with the username specified in the URL and the password "administrator".
 */
func (t *Developer) createadmin(response http.ResponseWriter, request *http.Request) {
	myuser := datastore.User{}
	myuser.Username = mux.Vars(request)["username"]
	myuser.Password = "******"
	myuser.IsAdmin = true
	helper, err := datastore.GetUserHelper()
	if err != nil {
		log.Println("DB Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	err = helper.SetUser(&myuser)
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	return
}