Beispiel #1
0
func CurrentUser(r *http.Request) *user.User {
	if id, ok := sessions.GetSession(r).Get("user_id").(int); ok {
		return user.Find(id)
	} else {
		return nil
	}
}
func IndexController(w http.ResponseWriter, r *http.Request) {
	d := make(map[string]interface{})
	jobs := job.FindByUserId(helpers.CurrentUser(r).Id)

	c := make(chan *user.User)
	for _, j := range jobs {
		go func(userId int) {
			c <- user.Find(userId)
		}(j.UserId)
	}

	// TODO Dirty as all hell.  Probably want to clean up.
	// Right now, it works.
	users := make([]*user.User, 0)
	for _ = range jobs {
		u := <-c
		if u == nil {
			continue
		}
		var b bool
		for _, v := range users {
			if u.Id == v.Id {
				b = true
				break
			}
		}
		if !b {
			users = append(users, u)
		}
	}

	d["jobs"] = jobs
	d["users"] = users
	w.Write(util.JSON(d))
}
func UserEditController(w http.ResponseWriter, r *http.Request) {
	id := getId(r)
	u := user.Find(id)
	// TODO Render 404 if user not found.
	templates.Render(w, r, "admin/user/edit", map[string]interface{}{
		"User": u,
	})
}
func UserUpdateController(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	id := getId(r)
	u := user.Find(id)
	updateAttributes(u, r)

	if u.Save() {
		flash.Success(r, "Account successfully updated!")
		http.Redirect(w, r, "/admin/users", http.StatusFound)
	} else {
		data := make(map[string]interface{})
		data["User"] = u
		// Render form to fix
		templates.Render(w, r, "admin/user/edit", map[string]interface{}{
			"User": u,
		})
	}
}
func UserDeleteController(w http.ResponseWriter, r *http.Request) {
	id := getId(r)
	u := user.Find(id)
	u.Delete()
	http.Redirect(w, r, "/admin/users", http.StatusFound)
}