Exemplo n.º 1
0
func (h *handler) RemoveFromFavPostHandler(c *echo.Context) error {

	s := session.Default(c)
	if s.Get("user") == "" {
		return c.JSON(http.StatusForbidden, "Access denied")
	}
	user, err := h.back.Model.User.GetByLogin(s.Get("user").(string))
	if (err != nil || user == &model.UserRow{}) {
		return c.JSON(http.StatusBadRequest, err.Error())
	}
	p, err := h.back.Model.Package.GetItem(c.Form("package"))
	if err != nil {
		log.Println(err)
		return c.JSON(http.StatusBadRequest, err.Error())
	}

	var packages []bson.ObjectId
	for _, up := range user.Packages {
		if up != p.Id {
			packages = append(packages, up)
		}
	}
	user.Packages = packages
	_, err = h.back.Model.User.Upsert(user)
	if err != nil {
		log.Println(err)
		return c.JSON(http.StatusBadRequest, err.Error())
	}

	return c.JSON(http.StatusOK, nil)
}
Exemplo n.º 2
0
func (h *handler) AddToFavPostHandler(c *echo.Context) error {
	s := session.Default(c)
	if s.Get("user") == "" {
		return c.JSON(http.StatusForbidden, "Access denied")
	}
	user, err := h.back.Model.User.GetByLogin(s.Get("user").(string))
	if (err != nil || user == &model.UserRow{}) {
		return c.JSON(http.StatusBadRequest, err.Error())
	}

	p, err := h.back.Model.Package.GetItem(c.Form("package"))
	if err != nil {
		log.Println(err)
		return c.JSON(http.StatusBadRequest, err.Error())
	}

	user.Packages = append(user.Packages, p.Id)
	_, err = h.back.Model.User.Upsert(user)
	if err != nil {
		log.Println(err)
		return c.JSON(http.StatusBadRequest, err.Error())
	}

	return c.JSON(http.StatusOK, nil)
}
Exemplo n.º 3
0
func (h *handler) OauthCallbackHandler(c *echo.Context) error {
	code := c.Query("code")
	token, err := oauthConf.Exchange(oauth2.NoContext, code)
	if err != nil {
		log.Println(err.Error(), "Can't exchange token")
		return c.Redirect(http.StatusTemporaryRedirect, "/")
	}

	oauthClient := oauthConf.Client(oauth2.NoContext, token)
	client := github.NewClient(oauthClient)
	user, _, err := client.Users.Get("")
	if err != nil {
		log.Println(err.Error(), "Can't get user")
		return c.Redirect(http.StatusTemporaryRedirect, "/")
	}

	userLogin := *user.Login
	userToken := token.AccessToken

	u := &model.UserRow{
		Login:     userLogin,
		Token:     userToken,
		AvatarURL: *user.AvatarURL,
	}
	ci, err := h.back.Model.User.CreateOrUpdate(u)
	if err != nil {
		log.Println(err.Error(), "Can't create user")
		return c.Redirect(http.StatusTemporaryRedirect, "/")
	}

	s := session.Default(c)
	if ci.Updated == 0 {
		s.Set("just_signup", true)
	} else {
		u, err = h.back.Model.User.GetByLogin(userLogin)
		if err != nil {
			return c.Redirect(http.StatusTemporaryRedirect, "/")
		}
	}

	var buf bytes.Buffer
	enc := json.NewEncoder(&buf)
	enc.Encode(*u)

	s.Set("username", userLogin)
	s.Set("user", buf.String())
	// hack to display username in header
	http.SetCookie(c.Response(), &http.Cookie{Name: "username", Value: userLogin, Path: "/"})
	http.SetCookie(c.Response(), &http.Cookie{Name: "useravatar", Value: *user.AvatarURL, Path: "/"})
	s.Set("token", userToken)
	s.Save()

	return c.Redirect(http.StatusFound, "/dashboard")
}
Exemplo n.º 4
0
func (h *handler) OauthRequestHandler(c *echo.Context) error {
	s := session.Default(c)
	if s.Get("user") != nil {
		return c.Redirect(http.StatusFound, "/dashboard")
	}

	oauthConf.ClientID = h.cfg.Github.ClientId
	oauthConf.ClientSecret = h.cfg.Github.ClientSecret
	oauthStateString := "random_string"
	url := oauthConf.AuthCodeURL(oauthStateString, oauth2.AccessTypeOnline)
	return c.Redirect(http.StatusTemporaryRedirect, url)
}
Exemplo n.º 5
0
func frontendMw(f *Frontend) echo.MiddlewareFunc {
	return func(h echo.HandlerFunc) echo.HandlerFunc {
		return func(c *echo.Context) error {
			s := session.Default(c)
			c.Set("session", s)

			if c.Get("user") == nil && s.Get("user") != nil {
				var u model.UserRow
				r := strings.NewReader(s.Get("user").(string))
				enc := json.NewDecoder(r)
				if err := enc.Decode(&u); err == nil {
					c.Set("user", u)
				}
			}

			return h(c)
		}
	}
}
Exemplo n.º 6
0
func (h *handler) DashboardGetHandler(c *echo.Context) error {
	s := session.Default(c)
	if s.Get("user") == "" {
		return c.Redirect(http.StatusTemporaryRedirect, "/")
	}
	user, err := h.back.Model.User.GetByLogin(s.Get("user").(string))
	if (err != nil || user == &model.UserRow{}) {
		return c.Redirect(http.StatusTemporaryRedirect, "/")
	}
	if s.Get("just_signup") != nil {
		packages, err := addUserRepos(h, user)
		if err != nil {
			log.Println(err)
			return c.Redirect(http.StatusTemporaryRedirect, "/")
		}
		var up []bson.ObjectId
		for _, p := range packages {
			up = append(up, p.Id)
		}
		user.Packages = up
		_, err = h.back.Model.User.Upsert(user)
		if err != nil {
			log.Println(err)
			return c.Redirect(http.StatusTemporaryRedirect, "/")
		}
		s.Delete("just_signup")
		s.Save()
	}
	packages, err := h.back.Model.Package.GetItemsByIdSlice(user.Packages)
	if err != nil {
		log.Println(err)
	}

	data := struct {
		Packages []model.PackageRow
	}{
		packages,
	}
	return c.Render(http.StatusOK, "dashboard.html", data)

}