// Password Change password of logged user func (*UsersController) Password(c echo.Context) error { type pl struct { Oldp string `json:"old_password"` Newp string `json:"new_password"` Conp string `json:"confirm_password"` } var payload pl var model models.User if err := c.Bind(&payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } if payload.Newp != payload.Conp { return c.JSON(400, utils.ErrMarshal("Password did not match.")) } userID := int(c.Get("userID").(float64)) ret, err := model.ChangePassword(userID, payload.Oldp, payload.Newp) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
// Login POST /auth/login func (*UsersController) Login(c echo.Context) error { var payload models.LoginPayload var user models.User an, _, err := net.SplitHostPort(c.Request().RemoteAddress()) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } if err = c.Bind(&payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } ret, err := user.Login(payload) if err != nil { msg := err.Error() // login failed err = utils.ST.Infraction(an, "Login") if err != nil { msg = msg + " " + err.Error() return c.JSON(400, utils.ErrMarshal(msg)) } // auth fails, increment infraction _, err = utils.ST.Strikes(an, "Login") return c.JSON(400, utils.ErrMarshal(msg)) } return c.JSON(200, ret) }
// Delegate refresh tokens POST /auth/delegation func (*UsersController) Delegate(c echo.Context) error { var payload models.DelegatePayload if err := c.Bind(&payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } ret, err := models.Delegate(payload) if err != nil { return c.JSON(401, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
func (*ContentController) Save(c echo.Context) error { var payload models.ContentPayload if err := c.Bind(&payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } iss := 1 payload.Iss = iss if ret, err := models.CreateContent(payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } else { return c.JSON(200, ret) } }
// Update tag func (*TagsController) Update(c echo.Context) error { var model models.Tag var payload models.Tag if err := c.Bind(&payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } iss := 1 payload.UpdatedBy = iss ret, err := model.Update(c.P(0), payload) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
// GetTime current func (*AdminController) GetTime(c echo.Context) error { ret, err := models.GetTime() if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
// Jailer login func Jailer(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { // snag the IP for use as the actor's name an, _, err := net.SplitHostPort(c.Request().RemoteAddress()) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } // if the Actor is jailed, send them StatusUnauthorized if utils.ST.IsJailed(an) { return c.JSON(400, utils.ErrMarshal("Too many login attempts. Please try again in "+strconv.Itoa(utils.STSentence)+" minutes.")) } // call the next middleware in the chain return next(c) } }
// Update post func (*PostsController) Update(c echo.Context) error { var model models.Post var payload models.PostPayload var status = c.QueryParam("status") if err := c.Bind(&payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } iss := 1 payload.UpdatedBy = iss ret, err := model.Update(c.P(0), payload, status) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
// GetOne tag func (*TagsController) GetOne(c echo.Context) error { var model models.Tag ret, err := model.GetOne(c.P(0)) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
// Get all tags func (*TagsController) Get(c echo.Context) error { var model models.Tag var name = c.QueryParam("name") ret, err := model.Get(name) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
// Get posts func (*PostsController) Get(c echo.Context) error { var model models.Post var title = c.QueryParam("title") ret, err := model.Get(title) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }
// Logout POST /auth/logout func (*UsersController) Logout(c echo.Context) error { type pl struct { Rft string } var payload pl if err := c.Bind(&payload); err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } userID := int(c.Get("userID").(float64)) ret, err := models.DeleteRefreshToken(userID, payload.Rft) if err != nil { return c.JSON(400, utils.ErrMarshal(err.Error())) } return c.JSON(200, ret) }