mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
Refactor out c.RequireSignedIn()
This commit is contained in:
@ -141,17 +141,14 @@ func (c *ApiController) Logout() {
|
||||
// @Success 200 {object} controllers.Response The Response object
|
||||
// @router /get-account [get]
|
||||
func (c *ApiController) GetAccount() {
|
||||
var resp Response
|
||||
|
||||
if c.GetSessionUser() == "" {
|
||||
resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
userId, ok := c.RequireSignedIn()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
username := c.GetSessionUser()
|
||||
user := object.GetUser(username)
|
||||
var resp Response
|
||||
|
||||
user := object.GetUser(userId)
|
||||
organization := object.GetOrganizationByUser(user)
|
||||
resp = Response{Status: "ok", Msg: "", Data: user, Data2: organization}
|
||||
|
||||
@ -166,17 +163,14 @@ func (c *ApiController) GetAccount() {
|
||||
// @Success 200 {object} controllers.Response The Response object
|
||||
// @router /upload-avatar [post]
|
||||
func (c *ApiController) UploadAvatar() {
|
||||
var resp Response
|
||||
|
||||
username := c.GetSessionUser()
|
||||
if c.GetSessionUser() == "" {
|
||||
resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
userId, ok := c.RequireSignedIn()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
user := object.GetUser(username)
|
||||
var resp Response
|
||||
|
||||
user := object.GetUser(userId)
|
||||
|
||||
avatarBase64 := c.Ctx.Request.Form.Get("avatarfile")
|
||||
index := strings.Index(avatarBase64, ",")
|
||||
|
@ -25,15 +25,13 @@ type LinkForm struct {
|
||||
}
|
||||
|
||||
func (c *ApiController) Unlink() {
|
||||
var resp Response
|
||||
|
||||
if c.GetSessionUser() == "" {
|
||||
resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
userId, ok := c.RequireSignedIn()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
var resp Response
|
||||
|
||||
var form LinkForm
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
|
||||
if err != nil {
|
||||
@ -41,7 +39,6 @@ func (c *ApiController) Unlink() {
|
||||
}
|
||||
providerType := form.ProviderType
|
||||
|
||||
userId := c.GetSessionUser()
|
||||
user := object.GetUser(userId)
|
||||
value := object.GetUserField(user, providerType)
|
||||
|
||||
|
@ -57,3 +57,14 @@ func (c *ApiController) ResponseError(error string) {
|
||||
c.Data["json"] = Response{Status: "error", Msg: error}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *ApiController) RequireSignedIn() (string, bool) {
|
||||
userId := c.GetSessionUser()
|
||||
if userId == "" {
|
||||
resp := Response{Status: "error", Msg: "Please sign in first"}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return "", false
|
||||
}
|
||||
return userId, true
|
||||
}
|
||||
|
@ -23,11 +23,11 @@ import (
|
||||
)
|
||||
|
||||
func (c *ApiController) SendVerificationCode() {
|
||||
userId := c.GetSessionUser()
|
||||
if len(userId) == 0 {
|
||||
c.ResponseError("Please sign in first")
|
||||
userId, ok := c.RequireSignedIn()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
user := object.GetUser(userId)
|
||||
if user == nil {
|
||||
c.ResponseError("No such user.")
|
||||
@ -77,11 +77,11 @@ func (c *ApiController) SendVerificationCode() {
|
||||
}
|
||||
|
||||
func (c *ApiController) ResetEmailOrPhone() {
|
||||
userId := c.GetSessionUser()
|
||||
if len(userId) == 0 {
|
||||
c.ResponseError("Please sign in first")
|
||||
userId, ok := c.RequireSignedIn()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
user := object.GetUser(userId)
|
||||
if user == nil {
|
||||
c.ResponseError("No such user.")
|
||||
|
Reference in New Issue
Block a user