Прикрутил роли

This commit is contained in:
Александр Кирюхин 2024-01-21 02:20:59 +03:00
parent 05a70d8578
commit 5256756411
8 changed files with 152 additions and 79 deletions

View file

@ -38,7 +38,6 @@ paths:
$ref: '#/components/responses/errorResponse'
/user/logout:
post:
security: []
responses:
204:
description: "success logout"
@ -48,7 +47,6 @@ paths:
# Game routes
/games:
get:
security: []
responses:
200:
$ref: '#/components/responses/gameListResponse'
@ -91,8 +89,9 @@ paths:
$ref: '#/components/responses/taskResponse'
/file/upload:
post:
security: []
operationId: uploadFile
security:
- cookieAuth: [creator, admin]
requestBody:
content:
multipart/form-data:
@ -135,7 +134,36 @@ components:
format: uuid
username:
type: string
required: [ id, username ]
email:
type: string
experience:
type: integer
level:
type: integer
expToCurrentLevel:
type: integer
expToNextLevel:
type: integer
games:
type: array
items:
$ref: "#/components/schemas/gameView"
role:
type: string
enum:
- user
- creator
- admin
required:
- id
- username
- email
- experience
- level
- expToCurrentLevel
- expToNextLevel
- games
- role
gameView:
type: object
properties:
@ -339,36 +367,7 @@ components:
content:
'application/json':
schema:
type: object
properties:
id:
type: string
format: uuid
username:
type: string
email:
type: string
experience:
type: integer
level:
type: integer
expToCurrentLevel:
type: integer
expToNextLevel:
type: integer
games:
type: array
items:
$ref: "#/components/schemas/gameView"
required:
- id
- username
- email
- experience
- level
- expToCurrentLevel
- expToNextLevel
- games
$ref: "#/components/schemas/userView"
errorResponse:
description: ''
content:

View file

@ -98,6 +98,8 @@ func (w *ServerInterfaceWrapper) EnterCode(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) UploadFile(ctx echo.Context) error {
var err error
ctx.Set(CookieAuthScopes, []string{"creator", "admin"})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.UploadFile(ctx)
return err
@ -125,6 +127,8 @@ func (w *ServerInterfaceWrapper) GetFile(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) GetGames(ctx echo.Context) error {
var err error
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetGames(ctx)
return err
@ -165,6 +169,8 @@ func (w *ServerInterfaceWrapper) PostUserLogin(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) PostUserLogout(ctx echo.Context) error {
var err error
ctx.Set(CookieAuthScopes, []string{})
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.PostUserLogout(ctx)
return err
@ -223,26 +229,26 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"H4sIAAAAAAAC/8xYS2/jNhD+KwXbozbyPk66bYNtUDQo2jTbS2AEXGnscCORKjn0xgj03ws+JMsWacmq",
"m+wpDjWaxzffjGb4THJR1YIDR0WyZyLhHw0KfxYFA3sAHEFeigJu3BNzlguOwO1PWtclyykywdOvSnBz",
"pvIHqKj5VUtRg0SvKhcFmL+4rYFkRKFkfE2aJrFWmYSCZHdOapm0UuLLV8iRNPtiKDU0CVnTCj4VDOf4",
"9pOEFcnIj+kOgNQ9VWmrN2K2FGvG/wMQUFFWBpBISE2V+iZkMQ6T09F7YyJkEtZMIciXdn/38F3wqVYg",
"Oa0mEKSTTIYg9K1MAsSeqFpw5WOTUsgbf3I+rjOOsAZpAq1AKbqeWgg7+XA4Bahcstr4RDJCfE1cM4Wz",
"gmAIlZpSHX8z+GaseZeolHR7zKNZ3kxzImwUqXo8u1Gj9JhRXZeCFmegj9bMFtFKyIoiydxBMlYZRmgq",
"T0wZncHTeDOAp/pWXGopgeM1bKAM14MV+x2eRmRAMuB5pKYMGdQZ+JuQSbAnpIz7Or2RWdWBbtaLtrUU",
"QnMAXYvDNAY0ic9p16/sF2/qV/tA4VisfeEk9o13D2xqXsCNkP3uwz+wf9xMQljuHoxSpxbMz1pD7pj+",
"Mp3HRtpNKUMeI8MyjJc7GK+QWyN3iJ9Tmxyk06psve8i9JjEYL71fgDXlVG9YRI1NSzOGW57r+1c7wp3",
"kB2q8UHI6dCZqou1gFwCRSg+4gy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uNBwn6JPd/0XYDngAvjOaFVWOouQaY+LgVcYxMD2S",
"wR5Nn4XkehecH4NPJKd6fTD3dmvShrhbjbfDhlOv9zxNVAqgp+i7lcnInKxSBHxlUAMv+vPydBQGo7IN",
"oM+e9gQZI+Rj0x9PDtvgqchaDOfnpb1Lf3RN/eTiOnyorZfC/dYcs2feETI/uTNxQQl0n1PqiL1bvZ3+",
"qP/a6ANOmuL2iTkcjT+UsVCvrNiCgDj99Xk8XZ3uaS94657fqsJZjpPcAP+7wcMJM1WagjE/eNVLEe8x",
"dp/Qj6O8biQXRKi18v8J0tGSW1PDNKC3TUuudM4SFtPNqF7X/wYAAP//R+pUyGkaAAA=",
}
// GetSwagger returns the content of the embedded swagger specification file

View file

@ -26,6 +26,13 @@ const (
OldCode TaskViewMessage = "old_code"
)
// Defines values for UserViewRole.
const (
Admin UserViewRole = "admin"
Creator UserViewRole = "creator"
User UserViewRole = "user"
)
// CodeEdit defines model for codeEdit.
type CodeEdit struct {
Code string `json:"code"`
@ -98,10 +105,20 @@ type TaskViewMessage string
// UserView defines model for userView.
type UserView struct {
Id openapi_types.UUID `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
ExpToCurrentLevel int `json:"expToCurrentLevel"`
ExpToNextLevel int `json:"expToNextLevel"`
Experience int `json:"experience"`
Games []GameView `json:"games"`
Id openapi_types.UUID `json:"id"`
Level int `json:"level"`
Role UserViewRole `json:"role"`
Username string `json:"username"`
}
// UserViewRole defines model for UserView.Role.
type UserViewRole string
// ErrorResponse defines model for errorResponse.
type ErrorResponse struct {
Code int `json:"code"`
@ -123,16 +140,7 @@ type UploadResponse struct {
}
// UserResponse defines model for userResponse.
type UserResponse struct {
Email string `json:"email"`
ExpToCurrentLevel int `json:"expToCurrentLevel"`
ExpToNextLevel int `json:"expToNextLevel"`
Experience int `json:"experience"`
Games []GameView `json:"games"`
Id openapi_types.UUID `json:"id"`
Level int `json:"level"`
Username string `json:"username"`
}
type UserResponse = UserView
// EnterCodeRequest defines model for enterCodeRequest.
type EnterCodeRequest struct {

View file

@ -13,6 +13,7 @@ import NoMatch from './pages/NoMatch'
import Engine from './pages/Engine'
import Quests from './pages/Quests'
import User from './pages/User'
import { useRole } from './utils/roles'
const router = createBrowserRouter(
createRoutesFromElements(
@ -40,19 +41,17 @@ const router = createBrowserRouter(
element={<Auth><Engine /></Auth>}
loader={({ params }) => ajax(`/api/engine/${params.gameId}`).catch(x => { console.log(x); return null })}
/>
{/* <Route
<Route
path="admin"
element={<Auth role="creator"><Admin /></Auth>}
loader={() => ajax(`/api/admin/games`)}
element={<Auth role="admin"><NoMatch /></Auth>}
// loader={() => ajax(`/api/admin/games`)}
/>
<Route
path="admin/games/new"
element={<Auth role="creator"><AdminGame /></Auth>}
loader={() => ({
title: "Новая игра",
tasks: []
})}
/> */}
<Route
path="quest/new"
element={<Auth role="creator"><NoMatch /></Auth>}
// loader={() => ajax(`/api/admin/games`)}
/>
<Route path="*" element={<NoMatch />} />
</Route>
)
@ -66,16 +65,22 @@ function App () {
function Auth (props) {
const baseUser = useRouteLoaderData('root')
const { user } = UserProvider.useContainer()
const { hasRole } = useRole()
const location = useLocation()
if (!user && !baseUser) {
return <Navigate to="/login" state={{ from: location }} replace />
}
if (props.role && !hasRole(props.role)) {
return <Navigate to="/" replace />
}
return props.children
}
Auth.propTypes = {
children: PropTypes.any
children: PropTypes.any,
role: PropTypes.string
}
export default App

View file

@ -5,6 +5,7 @@ import { ajax } from '../utils/fetch'
import { Layout, Menu } from 'antd'
import { MenuOutlined } from '@ant-design/icons'
import { Content, Header } from 'antd/es/layout/layout'
import { useRole } from '../utils/roles'
const AppLayout = () => {
const params = useLoaderData()
@ -14,6 +15,7 @@ const AppLayout = () => {
setUser(params)
}, [params])
const { user, setUser } = UserProvider.useContainer()
const { hasRole } = useRole()
const logout = () => {
ajax('/api/user/logout', {
@ -33,6 +35,20 @@ const AppLayout = () => {
label: 'Квесты',
link: '/quests'
},
hasRole('creator')
? {
key: 'quest/new',
label: 'Создать квест',
link: '/quest/new'
}
: null,
hasRole('admin')
? {
key: 'admin',
label: 'Админка',
link: '/admin'
}
: null,
{
key: 'me',
label: `${user.username} [${user.level}]`,

17
main.go
View file

@ -84,6 +84,23 @@ func main() {
echoCtx := ctx.Value(oapiMiddleware.EchoContextKey).(echo.Context)
user := appmiddleware.GetUser(echoCtx)
if user != nil {
if len(ai.Scopes) > 0 {
for _, v := range ai.Scopes {
switch v {
case "user":
return nil
case "creator":
if user.HasRole(models.RoleCreator) {
return nil
}
case "admin":
if user.HasRole(models.RoleAdmin) {
return nil
}
}
}
return echo.ErrForbidden
}
return nil
}

View file

@ -138,7 +138,15 @@ func mapUser(c echo.Context, user *models.User) error {
}
level := utils.ExpToLevel(user.Experience)
role := api.User
switch user.Role {
case models.RoleUser:
role = api.User
case models.RoleCreator:
role = api.Creator
case models.RoleAdmin:
role = api.Admin
}
return c.JSON(http.StatusOK, &api.UserResponse{
Id: user.ID,
Username: user.Username,
@ -148,5 +156,6 @@ func mapUser(c echo.Context, user *models.User) error {
ExpToNextLevel: utils.LevelToExp(level + 1),
Level: int(level),
Games: games,
Role: role,
})
}

View file

@ -13,4 +13,17 @@ type User struct {
Password string `json:"-"`
Experience int
Games []*GameCursor
Role UserRole
}
func (u *User) HasRole(role UserRole) bool {
return u.Role >= role
}
type UserRole int
const (
RoleUser UserRole = iota
RoleCreator
RoleAdmin
)