Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
fbc6bb21
Commit
fbc6bb21
authored
May 18, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More refactoring of user http api, trying to reuse handlers for sign in user and admin operations
parent
62e8841e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
44 additions
and
63 deletions
+44
-63
pkg/api/admin_users.go
+0
-46
pkg/api/api.go
+3
-4
pkg/api/common.go
+14
-3
pkg/api/user.go
+20
-4
pkg/services/sqlstore/user.go
+5
-4
public/app/features/admin/adminEditUserCtrl.js
+2
-2
No files found.
pkg/api/admin_users.go
View file @
fbc6bb21
...
...
@@ -19,26 +19,6 @@ func AdminSearchUsers(c *middleware.Context) {
c
.
JSON
(
200
,
query
.
Result
)
}
func
AdminGetUser
(
c
*
middleware
.
Context
)
{
userId
:=
c
.
ParamsInt64
(
":id"
)
query
:=
m
.
GetUserByIdQuery
{
Id
:
userId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to fetch user"
,
err
)
return
}
result
:=
dtos
.
AdminUserListItem
{
Name
:
query
.
Result
.
Name
,
Email
:
query
.
Result
.
Email
,
Login
:
query
.
Result
.
Login
,
IsGrafanaAdmin
:
query
.
Result
.
IsAdmin
,
}
c
.
JSON
(
200
,
result
)
}
func
AdminCreateUser
(
c
*
middleware
.
Context
,
form
dtos
.
AdminCreateUserForm
)
{
cmd
:=
m
.
CreateUserCommand
{
Login
:
form
.
Login
,
...
...
@@ -70,32 +50,6 @@ func AdminCreateUser(c *middleware.Context, form dtos.AdminCreateUserForm) {
c
.
JsonOK
(
"User created"
)
}
func
AdminUpdateUser
(
c
*
middleware
.
Context
,
form
dtos
.
AdminUpdateUserForm
)
{
userId
:=
c
.
ParamsInt64
(
":id"
)
cmd
:=
m
.
UpdateUserCommand
{
UserId
:
userId
,
Login
:
form
.
Login
,
Email
:
form
.
Email
,
Name
:
form
.
Name
,
}
if
len
(
cmd
.
Login
)
==
0
{
cmd
.
Login
=
cmd
.
Email
if
len
(
cmd
.
Login
)
==
0
{
c
.
JsonApiErr
(
400
,
"Validation error, need specify either username or email"
,
nil
)
return
}
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"failed to update user"
,
err
)
return
}
c
.
JsonOK
(
"User updated"
)
}
func
AdminUpdateUserPassword
(
c
*
middleware
.
Context
,
form
dtos
.
AdminUpdateUserPasswordForm
)
{
userId
:=
c
.
ParamsInt64
(
":id"
)
...
...
pkg/api/api.go
View file @
fbc6bb21
...
...
@@ -56,7 +56,7 @@ func Register(r *macaron.Macaron) {
// user
r
.
Group
(
"/user"
,
func
()
{
r
.
Get
(
"/"
,
wrap
(
GetSignedInUser
))
r
.
Put
(
"/"
,
bind
(
m
.
UpdateUserCommand
{}),
UpdateUser
)
r
.
Put
(
"/"
,
bind
(
m
.
UpdateUserCommand
{}),
wrap
(
UpdateSignedInUser
)
)
r
.
Post
(
"/using/:id"
,
UserSetUsingOrg
)
r
.
Get
(
"/orgs"
,
wrap
(
GetSignedInUserOrgList
))
r
.
Post
(
"/stars/dashboard/:id"
,
StarDashboard
)
...
...
@@ -66,8 +66,9 @@ func Register(r *macaron.Macaron) {
// users
r
.
Group
(
"/users"
,
func
()
{
r
.
Get
(
"/:id
/
"
,
wrap
(
GetUserById
))
r
.
Get
(
"/:id"
,
wrap
(
GetUserById
))
r
.
Get
(
"/:id/org"
,
wrap
(
GetUserOrgList
))
r
.
Put
(
"/:id"
,
bind
(
m
.
UpdateUserCommand
{}),
wrap
(
UpdateUser
))
},
reqGrafanaAdmin
)
// account
...
...
@@ -122,9 +123,7 @@ func Register(r *macaron.Macaron) {
r
.
Group
(
"/api/admin"
,
func
()
{
r
.
Get
(
"/settings"
,
AdminGetSettings
)
r
.
Get
(
"/users"
,
AdminSearchUsers
)
r
.
Get
(
"/users/:id"
,
AdminGetUser
)
r
.
Post
(
"/users"
,
bind
(
dtos
.
AdminCreateUserForm
{}),
AdminCreateUser
)
r
.
Put
(
"/users/:id/details"
,
bind
(
dtos
.
AdminUpdateUserForm
{}),
AdminUpdateUser
)
r
.
Put
(
"/users/:id/password"
,
bind
(
dtos
.
AdminUpdateUserPasswordForm
{}),
AdminUpdateUserPassword
)
r
.
Put
(
"/users/:id/permissions"
,
bind
(
dtos
.
AdminUpdateUserPermissionsForm
{}),
AdminUpdateUserPermissions
)
r
.
Delete
(
"/users/:id"
,
AdminDeleteUser
)
...
...
pkg/api/common.go
View file @
fbc6bb21
...
...
@@ -26,12 +26,17 @@ type NormalResponse struct {
header
http
.
Header
}
func
wrap
(
action
func
(
c
*
middleware
.
Context
)
Response
)
macaron
.
Handler
{
func
wrap
(
action
interface
{})
macaron
.
Handler
{
return
func
(
c
*
middleware
.
Context
)
{
res
:=
action
(
c
)
if
res
==
nil
{
var
res
Response
val
,
err
:=
c
.
Invoke
(
action
)
if
err
==
nil
&&
val
!=
nil
&&
len
(
val
)
>
0
{
res
=
val
[
0
]
.
Interface
()
.
(
Response
)
}
else
{
res
=
ServerError
}
res
.
WriteTo
(
c
.
Resp
)
}
}
...
...
@@ -64,6 +69,12 @@ func Json(status int, body interface{}) *NormalResponse {
return
Respond
(
status
,
body
)
.
Header
(
"Content-Type"
,
"application/json"
)
}
func
ApiSuccess
(
message
string
)
*
NormalResponse
{
resp
:=
make
(
map
[
string
]
interface
{})
resp
[
"message"
]
=
message
return
Respond
(
200
,
resp
)
}
func
ApiError
(
status
int
,
message
string
,
err
error
)
*
NormalResponse
{
resp
:=
make
(
map
[
string
]
interface
{})
...
...
pkg/api/user.go
View file @
fbc6bb21
...
...
@@ -27,15 +27,31 @@ func getUserUserProfile(userId int64) Response {
return
Json
(
200
,
query
.
Result
)
}
func
UpdateUser
(
c
*
middleware
.
Context
,
cmd
m
.
UpdateUserCommand
)
{
// POST /api/user
func
UpdateSignedInUser
(
c
*
middleware
.
Context
,
cmd
m
.
UpdateUserCommand
)
Response
{
cmd
.
UserId
=
c
.
UserId
return
handleUpdateUser
(
cmd
)
}
// POST /api/users/:id
func
UpdateUser
(
c
*
middleware
.
Context
,
cmd
m
.
UpdateUserCommand
)
Response
{
cmd
.
UserId
=
c
.
ParamsInt64
(
":id"
)
return
handleUpdateUser
(
cmd
)
}
func
handleUpdateUser
(
cmd
m
.
UpdateUserCommand
)
Response
{
if
len
(
cmd
.
Login
)
==
0
{
cmd
.
Login
=
cmd
.
Email
if
len
(
cmd
.
Login
)
==
0
{
return
ApiError
(
400
,
"Validation error, need specify either username or email"
,
nil
)
}
}
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
c
.
JsonApiErr
(
400
,
"Failed to update user"
,
err
)
return
return
ApiError
(
500
,
"failed to update user"
,
err
)
}
c
.
JsonOK
(
"User updated"
)
return
ApiSuccess
(
"User updated"
)
}
// GET /api/user/orgs
...
...
pkg/services/sqlstore/user.go
View file @
fbc6bb21
...
...
@@ -231,10 +231,11 @@ func GetUserProfile(query *m.GetUserProfileQuery) error {
}
query
.
Result
=
m
.
UserProfileDTO
{
Name
:
user
.
Name
,
Email
:
user
.
Email
,
Login
:
user
.
Login
,
Theme
:
user
.
Theme
,
Name
:
user
.
Name
,
Email
:
user
.
Email
,
Login
:
user
.
Login
,
Theme
:
user
.
Theme
,
IsGrafanaAdmin
:
user
.
IsAdmin
,
}
return
err
...
...
public/app/features/admin/adminEditUserCtrl.js
View file @
fbc6bb21
...
...
@@ -17,7 +17,7 @@ function (angular) {
};
$scope
.
getUser
=
function
(
id
)
{
backendSrv
.
get
(
'/api/
admin/
users/'
+
id
).
then
(
function
(
user
)
{
backendSrv
.
get
(
'/api/users/'
+
id
).
then
(
function
(
user
)
{
$scope
.
user
=
user
;
$scope
.
user_id
=
id
;
$scope
.
permissions
.
isGrafanaAdmin
=
user
.
isGrafanaAdmin
;
...
...
@@ -52,7 +52,7 @@ function (angular) {
$scope
.
update
=
function
()
{
if
(
!
$scope
.
userForm
.
$valid
)
{
return
;
}
backendSrv
.
put
(
'/api/
admin/users/'
+
$scope
.
user_id
+
'/details'
,
$scope
.
user
).
then
(
function
()
{
backendSrv
.
put
(
'/api/
users/'
+
$scope
.
user_id
,
$scope
.
user
).
then
(
function
()
{
$location
.
path
(
'/admin/users'
);
});
};
...
...
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