Пример #1
0
func Login(session sessions.Session, su models.User, r render.Render, p *models.Page) {

	// Check if we are already logged in
	if su.Id > 0 {
		r.Redirect(strings.Join([]string{utils.AppCfg.Url(), "albums"}, "/"), http.StatusFound)
		return
	}

	session.Set("loggedin", "false")

	// Init error holder
	errs := make(map[string]string)

	err_flash := session.Get("flash")
	if err_flash != nil {
		errs["flash"] = err_flash.(string)
		session.Set("flash", nil)
	}

	genform := utils.GenerateForm(&forms.Login{}, "/login", "POST", errs)

	p.SetUser(su)
	p.SetTitle("Login")
	p.Data = LoginVars{Form: genform}

	encoder.Render(p.Encoding, 200, "login", p, r)
}
Пример #2
0
func SetupAdmin(args martini.Params, session sessions.Session, r render.Render) {
	form := &forms.SetupAdmin{}
	session.Set("setup", "true")

	genform := utils.GenerateForm(form, "/setup", "POST", nil)
	r.HTML(200, "setup", genform)
}
Пример #3
0
func ImagePage(args martini.Params, su models.User, res http.ResponseWriter, req *http.Request, r render.Render, dbh *db.Dbh, p *models.Page) {
	name := args["name"]

	image := dbh.FirstImageByName(name)
	tags := dbh.GetAllTagsByImageId(image.Id)

	errs := make(map[string]string)
	form := utils.GenerateForm(&forms.Tag{Image: name}, "/tag", "POST", errs)

	p.SetUser(su)
	p.SetTitle("Image")

	src := fmt.Sprintf("%s/i/%s", utils.AppCfg.Url(), name)
	fullsrc := fmt.Sprintf("%s/o/%s", utils.AppCfg.Url(), name)
	p.Data = ImagePageVars{Name: src, Full: fullsrc, Tags: tags, Form: form}

	encoder.Render(p.Encoding, 200, "image", p, r)
}
Пример #4
0
func Account(su models.User, session sessions.Session, r render.Render, dbh *db.Dbh, p *models.Page) {

	// Init error holder
	errs := make(map[string]string)

	account := forms.Account{}
	account.Id = su.Id
	account.Email = su.Email
	account.Name = su.Name

	username := su.UserName
	if username == "" {
		username = genUserName(su.Email, dbh)
	}
	account.UserName = username
	genform := utils.GenerateForm(&account, "/account", "POST", errs)

	p.SetTitle("Account")
	p.SetUser(su)
	p.Data = accountVars{Form: genform}

	encoder.Render(p.Encoding, 200, "account", p, r)
}
Пример #5
0
func SetupAdminPost(sa forms.SetupAdmin, args martini.Params, session sessions.Session, r render.Render, res http.ResponseWriter, dbh *db.Dbh) {
	errs := Validate(&sa)
	if len(errs) > 0 {
		fmt.Printf("%+v\n", errs)
	}

	v := session.Get("setup")
	if v != "true" {
		http.Error(res, "Unauthorized", http.StatusUnauthorized)
		return
	}

	user := dbh.GetUserByEmail(sa.Email)

	if user.Id > 0 {
		fmt.Println("user already exists")
		session.Set("uid", user.Id)
	}

	if user.Id == 0 {
		fmt.Println("id: 0")
		hash, salt, err := auth.EncryptPassword(sa.Password)
		if err != nil {
			fmt.Println("hash err: ", err)
		}
		newuser := models.User{Name: sa.Name, Email: sa.Email, Password: hash, Salt: salt, Timestamp: time.Now().Unix()}
		dbh.InsertUser(newuser)
		session.Set("uid", newuser.Id)
	}

	uid := session.Get("uid")

	fmt.Println("uid: ", uid)
	genform := utils.GenerateForm(&sa, "/setup", "POST", errs)
	r.HTML(200, "setup", genform)
}