Esempio n. 1
0
/**
 * Show edit template
 * Route /users/edit/profile/
 */
func (u *UserController) Edit(r render.Render, di *di.DI, templateVars utils.TemplateVars) {
	authManager := di.AuthManager()
	if !authManager.IsAuthenticated() {
		r.HTML(403, "error/403", templateVars)
		return
	}
	currentUser := authManager.CurrentUser()
	templateVars.SetData(currentUser)
	r.HTML(200, "user/edit", templateVars)
}
Esempio n. 2
0
/**
 * Route /users/:id/view/
 */
func (u *UserController) View(params martini.Params, r render.Render, di *di.DI, templateVars utils.TemplateVars) {
	userManager := di.UserManager()
	id := params["id"]
	user := userManager.FindById(id)
	if user == nil {
		r.HTML(404, "error/404", templateVars)
	} else {
		templateVars.SetData(user)
		r.HTML(200, "user/view", templateVars)
	}
}
Esempio n. 3
0
// Show login template
func (self *UpdateLogController) List(params martini.Params, r render.Render, di *di.DI, templateVars utils.TemplateVars) {
	authManager := di.AuthManager()
	if !authManager.IsAuthenticated() {
		r.HTML(403, "error/403", templateVars)
		return
	}
	currentUser := authManager.CurrentUser()

	userId := currentUser.Id

	logs := di.UpdateLogManager().FindAll(userId)
	templateVars.SetData(logs)
	r.HTML(200, "updateLog/list", templateVars)
}
Esempio n. 4
0
/**
 * Routes '/', '/users/'
 */
func (u *UserController) List(r render.Render, di *di.DI, templateVars utils.TemplateVars) {
	userManager := di.UserManager()
	all := userManager.FindAll()
	templateVars.SetData(all)
	r.HTML(200, "user/list", templateVars)
}
Esempio n. 5
0
func Run() {
	m := martini.Classic()

	m.Use(func(c martini.Context) {
		d := di.New()
		c.Map(d)
		c.Map(d.AuthManager())
	})

	// authenticate user
	m.Use(func(req *http.Request, manager *managers.AuthManager) {
		tokenCookie, err := req.Cookie("gousertoken")
		if err != nil {
			return
		}
		user, err := manager.FindActiveByToken(tokenCookie.Value)
		if err != nil || user == nil {
			return
		}
		manager.Auth(user)
		utils.Debug("user authenticated")
	})

	// setup renderer
	m.Use(render.Renderer(render.Options{
		Layout:    "layout",
		Directory: "templates",
		Funcs: []template.FuncMap{
			{
				"formatTime": func(t *time.Time) string {
					return t.Format(time.Stamp)
				},
			},
		},
	}))

	// predefined template vars
	m.Use(func(c martini.Context, manager *managers.AuthManager) {
		tplData := utils.TemplateVars{}
		tplData.AddVar("isAuthenticated", manager.IsAuthenticated())

		c.Map(tplData)
	})

	// after request actions
	m.Use(func(c martini.Context, di *di.DI) {
		c.Next()
		di.Db().Close()
		utils.Debug("DB connection closed")
	})

	userController := new(controllers.UserController)
	m.Get("/", userController.List)
	m.Get("/users/", userController.List)
	m.Get("/users/:id/view/", userController.View)
	m.Get("/users/edit/profile/", userController.Edit)
	m.Post("/users/save/profile/", userController.Save)

	registerController := new(controllers.RegisterController)
	m.Get("/register/", registerController.Register)
	m.Post("/register/processRegister/", registerController.ProcessRegister)
	m.Get("/register/activate/:hash/", registerController.ProcessActivate)

	authController := new(controllers.AuthController)
	m.Get("/auth/", authController.Login)
	m.Get("/auth/logout/", authController.LogOut)
	m.Post("/auth/processLogin/", authController.ProcessLogin)

	updatesController := new(controllers.UpdateLogController)
	m.Get("/updates/", updatesController.List)

	m.Run()
}