-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
529 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/RockChinQ/Campux/backend/database" | ||
"github.com/RockChinQ/Campux/backend/service" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type AdminRouter struct { | ||
APIRouter | ||
AdminService service.AdminService | ||
} | ||
|
||
func NewAdminRouter(rg *gin.RouterGroup, as service.AdminService) *AdminRouter { | ||
ar := &AdminRouter{ | ||
AdminService: as, | ||
} | ||
|
||
group := rg.Group("/admin") | ||
|
||
// bind routes | ||
group.POST("/add-oauth2-app", ar.AddOAuth2App) | ||
group.GET("/get-oauth2-apps", ar.GetOAuth2AppList) | ||
group.DELETE("/del-oauth2-app/:id", ar.DeleteOAuth2App) | ||
|
||
return ar | ||
} | ||
|
||
// 添加一个OAuth2应用 | ||
func (ar *AdminRouter) AddOAuth2App(c *gin.Context) { | ||
|
||
uin, err := ar.Auth(c, UserOnly) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if !ar.AdminService.CheckUserGroup(uin, []database.UserGroup{ | ||
database.USER_GROUP_ADMIN, | ||
}) { | ||
ar.StatusCode(c, 401, "权限不足") | ||
return | ||
} | ||
|
||
// 取body的json里的appname | ||
var body OAuth2AppCreateBody | ||
|
||
if err := c.ShouldBindJSON(&body); err != nil { | ||
ar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
// 创建OAuth2应用 | ||
app, err := ar.AdminService.AddOAuth2App(body.Name, body.Emoji) | ||
|
||
if err != nil { | ||
ar.Fail(c, 2, err.Error()) | ||
return | ||
} | ||
|
||
ar.Success(c, app) | ||
} | ||
|
||
// 获取 OAuth2 应用列表 | ||
func (ar *AdminRouter) GetOAuth2AppList(c *gin.Context) { | ||
uin, err := ar.Auth(c, UserOnly) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if !ar.AdminService.CheckUserGroup(uin, []database.UserGroup{ | ||
database.USER_GROUP_ADMIN, | ||
}) { | ||
ar.StatusCode(c, 401, "权限不足") | ||
return | ||
} | ||
|
||
// 获取OAuth2应用列表 | ||
list, err := ar.AdminService.GetOAuth2Apps() | ||
|
||
if err != nil { | ||
ar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
ar.Success(c, gin.H{ | ||
"list": list, | ||
}) | ||
} | ||
|
||
// 删除一个OAuth2应用 | ||
func (ar *AdminRouter) DeleteOAuth2App(c *gin.Context) { | ||
uin, err := ar.Auth(c, UserOnly) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if !ar.AdminService.CheckUserGroup(uin, []database.UserGroup{ | ||
database.USER_GROUP_ADMIN, | ||
}) { | ||
ar.StatusCode(c, 401, "权限不足") | ||
return | ||
} | ||
|
||
// 取路由参数 | ||
appID := c.Param("id") | ||
|
||
// 删除OAuth2应用 | ||
err = ar.AdminService.DeleteOAuth2App(appID) | ||
|
||
if err != nil { | ||
ar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
ar.Success(c, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/RockChinQ/Campux/backend/database" | ||
"github.com/RockChinQ/Campux/backend/util" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type AdminService struct { | ||
CommonService | ||
} | ||
|
||
func NewAdminService(db database.MongoDBManager) *AdminService { | ||
return &AdminService{ | ||
CommonService: CommonService{ | ||
DB: db, | ||
}, | ||
} | ||
} | ||
|
||
func (as *AdminService) AddOAuth2App(name, emoji string) (*database.OAuthAppPO, error) { | ||
check, err := as.DB.GetOAuth2AppByName(name) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if check != nil { | ||
return nil, ErrOAuth2AppAlreadyExist | ||
} | ||
|
||
app := &database.OAuthAppPO{ | ||
Name: name, | ||
Emoji: emoji, | ||
ClientID: util.RandomString(16), | ||
ClientSecret: uuid.New().String(), | ||
CreatedAt: util.GetCSTTime(), | ||
} | ||
|
||
err = as.DB.AddOAuth2App(app) | ||
|
||
return app, err | ||
} | ||
|
||
func (as *AdminService) GetOAuth2Apps() ([]database.OAuthAppPO, error) { | ||
return as.DB.GetOAuth2Apps() | ||
} | ||
|
||
// delete | ||
func (as *AdminService) DeleteOAuth2App(appID string) error { | ||
return as.DB.DeleteOAuth2App(appID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.