fix: improve code specification (#231)

This commit is contained in:
sh1luo
2021-08-07 22:02:56 +08:00
committed by Yang Luo
parent a271ef0719
commit 8c66ef6860
36 changed files with 209 additions and 164 deletions

View File

@ -72,6 +72,7 @@ type HumanCheck struct {
CaptchaImage interface{} `json:"captchaImage"`
}
// Signup
// @Title Signup
// @Description sign up a new user
// @Param username formData string true "The username to sign up"
@ -178,6 +179,7 @@ func (c *ApiController) Signup() {
c.ServeJSON()
}
// Logout
// @Title Logout
// @Description logout the current user
// @Success 200 {object} controllers.Response The Response object
@ -197,6 +199,7 @@ func (c *ApiController) Logout() {
c.ServeJSON()
}
// GetAccount
// @Title GetAccount
// @Description get the details of the current account
// @Success 200 {object} controllers.Response The Response object
@ -224,6 +227,7 @@ func (c *ApiController) GetAccount() {
c.ServeJSON()
}
// UploadAvatar
// @Title UploadAvatar
// @Description upload avatar
// @Param avatarfile formData string true "The base64 encode of avatarfile"
@ -272,6 +276,7 @@ func (c *ApiController) UploadAvatar() {
c.ServeJSON()
}
// GetHumanCheck ...
func (c *ApiController) GetHumanCheck() {
c.Data["json"] = HumanCheck{Type: "none"}

View File

@ -20,6 +20,7 @@ import (
"github.com/casbin/casdoor/object"
)
// GetApplications
// @Title GetApplications
// @Description get all applications
// @Param owner query string true "The owner of applications."
@ -32,6 +33,7 @@ func (c *ApiController) GetApplications() {
c.ServeJSON()
}
// GetApplication
// @Title GetApplication
// @Description get the detail of an application
// @Param id query string true "The id of the application."
@ -44,6 +46,7 @@ func (c *ApiController) GetApplication() {
c.ServeJSON()
}
// GetUserApplication
// @Title GetUserApplication
// @Description get the detail of the user's application
// @Param id query string true "The id of the user"
@ -61,6 +64,7 @@ func (c *ApiController) GetUserApplication() {
c.ServeJSON()
}
// UpdateApplication
// @Title UpdateApplication
// @Description update an application
// @Param id query string true "The id of the application"
@ -80,6 +84,7 @@ func (c *ApiController) UpdateApplication() {
c.ServeJSON()
}
// AddApplication
// @Title AddApplication
// @Description add an application
// @Param body body object.Application true "The details of the application"
@ -96,6 +101,7 @@ func (c *ApiController) AddApplication() {
c.ServeJSON()
}
// DeleteApplication
// @Title DeleteApplication
// @Description delete an application
// @Param body body object.Application true "The details of the application"

View File

@ -30,14 +30,14 @@ import (
func codeToResponse(code *object.Code) *Response {
if code.Code == "" {
return &Response{Status: "error", Msg: code.Message, Data: code.Code}
} else {
return &Response{Status: "ok", Msg: "", Data: code.Code}
}
return &Response{Status: "ok", Msg: "", Data: code.Code}
}
func (c *ApiController) HandleLoggedIn(application *object.Application, user *object.User, form *RequestForm) *Response {
// HandleLoggedIn ...
func (c *ApiController) HandleLoggedIn(application *object.Application, user *object.User, form *RequestForm) (resp *Response) {
userId := user.GetId()
resp := &Response{}
if form.Type == ResponseTypeLogin {
c.SetSessionUsername(userId)
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
@ -72,6 +72,7 @@ func (c *ApiController) HandleLoggedIn(application *object.Application, user *ob
return resp
}
// GetApplicationLogin ...
// @Title GetApplicationLogin
// @Description get application login
// @Param clientId query string true "client id"
@ -108,6 +109,7 @@ func setHttpClient(idProvider idp.IdProvider, providerType string) {
}
}
// Login ...
// @Title Login
// @Description login
// @Param oAuthParams query string true "oAuth parameters"
@ -182,13 +184,11 @@ func (c *ApiController) Login() {
c.ResponseError("wrong email!")
}
object.DisableVerificationCode(form.Email)
break
case "phone":
if user.Phone != form.Email {
c.ResponseError("wrong phone!")
}
object.DisableVerificationCode(form.Email)
break
}
} else {
password := form.Password
@ -282,7 +282,7 @@ func (c *ApiController) Login() {
record.Organization = application.Organization
record.Username = user.Name
object.AddRecord(record)
object.AddRecord(record)
} else {
// Sign up via OAuth
if !application.EnableSignUp {

View File

@ -29,6 +29,7 @@ type SessionData struct {
ExpireTime int64
}
// GetSessionUsername ...
func (c *ApiController) GetSessionUsername() string {
// check if user session expired
sessionData := c.GetSessionData()
@ -48,10 +49,12 @@ func (c *ApiController) GetSessionUsername() string {
return user.(string)
}
// SetSessionUsername ...
func (c *ApiController) SetSessionUsername(user string) {
c.SetSession("username", user)
}
// GetSessionData ...
func (c *ApiController) GetSessionData() *SessionData {
session := c.GetSession("SessionData")
if session == nil {
@ -67,6 +70,7 @@ func (c *ApiController) GetSessionData() *SessionData {
return sessionData
}
// SetSessionData ...
func (c *ApiController) SetSessionData(s *SessionData) {
if s == nil {
c.DelSession("SessionData")

View File

@ -97,7 +97,6 @@ func (c *ApiController) GetLdapUser() {
c.Data["json"] = Response{Status: "ok", Data: resp}
c.ServeJSON()
return
}
func (c *ApiController) GetLdaps() {

View File

@ -24,6 +24,7 @@ type LinkForm struct {
ProviderType string `json:"providerType"`
}
// Unlink ...
func (c *ApiController) Unlink() {
userId, ok := c.RequireSignedIn()
if !ok {

View File

@ -20,6 +20,7 @@ import (
"github.com/casbin/casdoor/object"
)
// GetOrganizations ...
// @Title GetOrganizations
// @Description get organizations
// @Param owner query string true "owner"
@ -32,6 +33,7 @@ func (c *ApiController) GetOrganizations() {
c.ServeJSON()
}
// GetOrganization ...
// @Title GetOrganization
// @Description get organization
// @Param id query string true "organization id"
@ -44,6 +46,7 @@ func (c *ApiController) GetOrganization() {
c.ServeJSON()
}
// UpdateOrganization ...
// @Title UpdateOrganization
// @Description update organization
// @Param id query string true "The id of the organization"
@ -63,6 +66,7 @@ func (c *ApiController) UpdateOrganization() {
c.ServeJSON()
}
// AddOrganization ...
// @Title AddOrganization
// @Description add organization
// @Param body body object.Organization true "The details of the organization"
@ -79,6 +83,7 @@ func (c *ApiController) AddOrganization() {
c.ServeJSON()
}
// DeleteOrganization ...
// @Title DeleteOrganization
// @Description delete organization
// @Param body body object.Organization true "The details of the organization"

View File

@ -20,6 +20,7 @@ import (
"github.com/casbin/casdoor/object"
)
// GetProviders
// @Title GetProviders
// @Description get providers
// @Param owner query string true "The owner of providers"

View File

@ -20,6 +20,7 @@ import (
"github.com/casbin/casdoor/object"
)
// GetRecords
// @Title GetRecords
// @Description get all records
// @Success 200 {array} object.Records The Response object
@ -29,6 +30,7 @@ func (c *ApiController) GetRecords() {
c.ServeJSON()
}
// GetRecordsByFilter
// @Title GetRecordsByFilter
// @Description get records by filter
// @Param body body object.Records true "filter Record message"

View File

@ -25,6 +25,7 @@ import (
sender "github.com/casdoor/go-sms-sender"
)
// SendEmail
// @Title SendEmail
// @Description This API is not for Casdoor frontend to call, it is for Casdoor SDKs.
// @Param clientId query string true "The clientId of the application"
@ -84,8 +85,7 @@ func (c *ApiController) SendEmail() {
emailForm.Title,
emailForm.Content,
receiver,
emailForm.Sender);
len(msg) == 0 {
emailForm.Sender); len(msg) == 0 {
ok++
}
}
@ -94,6 +94,7 @@ func (c *ApiController) SendEmail() {
c.ServeJSON()
}
// SendSms
// @Title SendSms
// @Description This API is not for Casdoor frontend to call, it is for Casdoor SDKs.
// @Param clientId query string true "The clientId of the application"
@ -148,7 +149,7 @@ func (c *ApiController) SendSms() {
}
}
if len(invalidReceivers) != 0{
if len(invalidReceivers) != 0 {
c.ResponseError("Invalid phone numbers", invalidReceivers)
return
}

View File

@ -20,6 +20,7 @@ import (
"github.com/casbin/casdoor/object"
)
// GetTokens
// @Title GetTokens
// @Description get tokens
// @Param owner query string true "The owner of tokens"
@ -32,6 +33,7 @@ func (c *ApiController) GetTokens() {
c.ServeJSON()
}
// GetToken
// @Title GetToken
// @Description get token
// @Param id query string true "The id of token"
@ -44,6 +46,7 @@ func (c *ApiController) GetToken() {
c.ServeJSON()
}
// UpdateToken
// @Title UpdateToken
// @Description update token
// @Param id query string true "The id of token"
@ -63,6 +66,7 @@ func (c *ApiController) UpdateToken() {
c.ServeJSON()
}
// AddToken
// @Title AddToken
// @Description add token
// @Param body body object.Token true "Details of the token"
@ -79,6 +83,7 @@ func (c *ApiController) AddToken() {
c.ServeJSON()
}
// DeleteToken
// @Title DeleteToken
// @Description delete token
// @Param body body object.Token true "Details of the token"
@ -95,6 +100,7 @@ func (c *ApiController) DeleteToken() {
c.ServeJSON()
}
// GetOAuthToken
// @Title GetOAuthToken
// @Description get oAuth token
// @Param grant_type query string true "oAuth grant type"

View File

@ -23,6 +23,7 @@ import (
"github.com/casbin/casdoor/original"
)
// GetGlobalUsers
// @Title GetGlobalUsers
// @Description get global users
// @Success 200 {array} object.User The Response object
@ -32,6 +33,7 @@ func (c *ApiController) GetGlobalUsers() {
c.ServeJSON()
}
// GetUsers
// @Title GetUsers
// @Description
// @Param owner query string true "The owner of users"
@ -44,6 +46,7 @@ func (c *ApiController) GetUsers() {
c.ServeJSON()
}
// GetUser
// @Title GetUser
// @Description get user
// @Param id query string true "The id of the user"
@ -56,6 +59,7 @@ func (c *ApiController) GetUser() {
c.ServeJSON()
}
// UpdateUser
// @Title UpdateUser
// @Description update user
// @Param id query string true "The id of the user"
@ -86,6 +90,7 @@ func (c *ApiController) UpdateUser() {
c.ServeJSON()
}
// AddUser
// @Title AddUser
// @Description add user
// @Param body body object.User true "The details of the user"
@ -102,6 +107,7 @@ func (c *ApiController) AddUser() {
c.ServeJSON()
}
// DeleteUser
// @Title DeleteUser
// @Description delete user
// @Param body body object.User true "The details of the user"
@ -118,6 +124,7 @@ func (c *ApiController) DeleteUser() {
c.ServeJSON()
}
// GetEmailAndPhone
// @Title GetEmailAndPhone
// @Description get email and phone by username
// @Param username formData string true "The username of the user"
@ -156,6 +163,7 @@ func (c *ApiController) GetEmailAndPhone() {
c.ServeJSON()
}
// SetPassword
// @Title SetPassword
// @Description set password
// @Param userOwner formData string true "The owner of the user"
@ -209,11 +217,9 @@ func (c *ApiController) SetPassword() {
c.ResponseError(msg)
return
}
} else {
}
if strings.Index(newPassword, " ") >= 0 {
if strings.Contains(newPassword, " ") {
c.ResponseError("New password cannot contain blank space.")
return
}

View File

@ -24,6 +24,7 @@ import (
var defaultHttpClient *http.Client
var proxyHttpClient *http.Client
// InitHttpClient ...
func InitHttpClient() {
// not use proxy
defaultHttpClient = http.DefaultClient
@ -54,6 +55,7 @@ func InitHttpClient() {
//println("Response status: %s", resp.Status)
}
// ResponseError ...
func (c *ApiController) ResponseError(error string, data ...interface{}) {
resp := Response{Status: "error", Msg: error}
switch len(data) {
@ -67,11 +69,13 @@ func (c *ApiController) ResponseError(error string, data ...interface{}) {
c.ServeJSON()
}
// ResponseErrorWithData ...
func (c *ApiController) ResponseErrorWithData(error string, data interface{}) {
c.Data["json"] = Response{Status: "error", Msg: error, Data: data}
c.ServeJSON()
}
// RequireSignedIn ...
func (c *ApiController) RequireSignedIn() (string, bool) {
userId := c.GetSessionUsername()
if userId == "" {

View File

@ -33,6 +33,7 @@ func (c *ApiController) getCurrentUser() *object.User {
return user
}
// SendVerificationCode ...
func (c *ApiController) SendVerificationCode() {
destType := c.Ctx.Request.Form.Get("type")
dest := c.Ctx.Request.Form.Get("dest")
@ -42,7 +43,7 @@ func (c *ApiController) SendVerificationCode() {
checkKey := c.Ctx.Request.Form.Get("checkKey")
remoteAddr := util.GetIPFromRequest(c.Ctx.Request)
if len(destType) == 0 || len(dest) == 0 || len(orgId) == 0 || strings.Index(orgId, "/") < 0 || len(checkType) == 0 || len(checkId) == 0 || len(checkKey) == 0 {
if len(destType) == 0 || len(dest) == 0 || len(orgId) == 0 || !strings.Contains(orgId, "/") || len(checkType) == 0 || len(checkId) == 0 || len(checkKey) == 0 {
c.ResponseError("Missing parameter.")
return
}
@ -97,6 +98,7 @@ func (c *ApiController) SendVerificationCode() {
c.ServeJSON()
}
// ResetEmailOrPhone ...
func (c *ApiController) ResetEmailOrPhone() {
userId, ok := c.RequireSignedIn()
if !ok {