Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
9b2e57ce
Commit
9b2e57ce
authored
Sep 24, 2022
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: api access checks
parent
77a3513a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
4 deletions
+31
-4
memo.go
server/memo.go
+31
-4
No files found.
server/memo.go
View file @
9b2e57ce
...
...
@@ -20,6 +20,7 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing user in session"
)
}
memoCreate
:=
&
api
.
MemoCreate
{
CreatorID
:
userID
,
// Private is the default memo visibility.
...
...
@@ -28,7 +29,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
if
err
:=
json
.
NewDecoder
(
c
.
Request
()
.
Body
)
.
Decode
(
memoCreate
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Malformatted post memo request"
)
.
SetInternal
(
err
)
}
if
memoCreate
.
Content
==
""
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
"Memo content shouldn't be empty"
)
}
...
...
@@ -64,11 +64,24 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
g
.
PATCH
(
"/memo/:memoId"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing user in session"
)
}
memoID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"memoId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"ID is not a number: %s"
,
c
.
Param
(
"memoId"
)))
.
SetInternal
(
err
)
}
memoFind
:=
&
api
.
MemoFind
{
ID
:
&
memoID
,
CreatorID
:
&
userID
,
}
if
_
,
err
:=
s
.
Store
.
FindMemo
(
ctx
,
memoFind
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find memo"
)
.
SetInternal
(
err
)
}
memoPatch
:=
&
api
.
MemoPatch
{
ID
:
memoID
,
}
...
...
@@ -91,7 +104,6 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
g
.
GET
(
"/memo"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
memoFind
:=
&
api
.
MemoFind
{}
if
userID
,
err
:=
strconv
.
Atoi
(
c
.
QueryParam
(
"creatorId"
));
err
==
nil
{
memoFind
.
CreatorID
=
&
userID
}
...
...
@@ -222,10 +234,12 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
fmt
.
Sprintf
(
"Failed to find memo by ID: %v"
,
memoID
))
.
SetInternal
(
err
)
}
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
memo
.
Visibility
==
api
.
Privite
{
return
echo
.
NewHTTPError
(
http
.
StatusForbidden
,
"this memo is private only"
)
if
!
ok
||
memo
.
CreatorID
!=
userID
{
return
echo
.
NewHTTPError
(
http
.
StatusForbidden
,
"this memo is private only"
)
}
}
else
if
memo
.
Visibility
==
api
.
Protected
{
_
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusForbidden
,
"this memo is protected, missing user in session"
)
}
...
...
@@ -282,11 +296,24 @@ func (s *Server) registerMemoRoutes(g *echo.Group) {
g
.
DELETE
(
"/memo/:memoId"
,
func
(
c
echo
.
Context
)
error
{
ctx
:=
c
.
Request
()
.
Context
()
userID
,
ok
:=
c
.
Get
(
getUserIDContextKey
())
.
(
int
)
if
!
ok
{
return
echo
.
NewHTTPError
(
http
.
StatusUnauthorized
,
"Missing user in session"
)
}
memoID
,
err
:=
strconv
.
Atoi
(
c
.
Param
(
"memoId"
))
if
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusBadRequest
,
fmt
.
Sprintf
(
"ID is not a number: %s"
,
c
.
Param
(
"memoId"
)))
.
SetInternal
(
err
)
}
memoFind
:=
&
api
.
MemoFind
{
ID
:
&
memoID
,
CreatorID
:
&
userID
,
}
if
_
,
err
:=
s
.
Store
.
FindMemo
(
ctx
,
memoFind
);
err
!=
nil
{
return
echo
.
NewHTTPError
(
http
.
StatusInternalServerError
,
"Failed to find memo"
)
.
SetInternal
(
err
)
}
memoDelete
:=
&
api
.
MemoDelete
{
ID
:
memoID
,
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment