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
1bd31aa3
Commit
1bd31aa3
authored
Jun 07, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
check if admin exists or create one in one transaction
parent
26357281
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
76 deletions
+91
-76
pkg/services/sqlstore/sqlstore.go
+20
-18
pkg/services/sqlstore/user.go
+71
-58
No files found.
pkg/services/sqlstore/sqlstore.go
View file @
1bd31aa3
...
...
@@ -143,30 +143,32 @@ func (ss *SqlStore) ensureAdminUser() error {
systemUserCountQuery
:=
m
.
GetSystemUserCountStatsQuery
{}
err
:=
bus
.
InTransaction
(
context
.
Background
(),
func
(
ctx
context
.
Context
)
error
{
return
bus
.
DispatchCtx
(
ctx
,
&
systemUserCountQuery
)
})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Could not determine if admin user exists: %v"
,
err
)
}
err
:=
bus
.
DispatchCtx
(
ctx
,
&
systemUserCountQuery
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Could not determine if admin user exists: %v"
,
err
)
}
if
systemUserCountQuery
.
Result
.
Count
>
0
{
return
nil
}
if
systemUserCountQuery
.
Result
.
Count
>
0
{
return
nil
}
cmd
:=
m
.
CreateUserCommand
{}
cmd
.
Login
=
setting
.
AdminUser
cmd
.
Email
=
setting
.
AdminUser
+
"@localhost"
cmd
.
Password
=
setting
.
AdminPassword
cmd
.
IsAdmin
=
true
cmd
:=
m
.
CreateUserCommand
{}
cmd
.
Login
=
setting
.
AdminUser
cmd
.
Email
=
setting
.
AdminUser
+
"@localhost"
cmd
.
Password
=
setting
.
AdminPassword
cmd
.
IsAdmin
=
true
if
err
:=
bus
.
Dispatch
(
&
cmd
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Failed to create admin user: %v"
,
err
)
}
if
err
:=
bus
.
DispatchCtx
(
ctx
,
&
cmd
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Failed to create admin user: %v"
,
err
)
}
ss
.
log
.
Info
(
"Created default admin user: %v
"
,
setting
.
AdminUser
)
ss
.
log
.
Info
(
"Created default admin"
,
"user
"
,
setting
.
AdminUser
)
return
nil
return
nil
})
return
err
}
func
(
ss
*
SqlStore
)
buildConnectionString
()
(
string
,
error
)
{
...
...
pkg/services/sqlstore/user.go
View file @
1bd31aa3
package
sqlstore
import
(
"context"
"strconv"
"strings"
"time"
...
...
@@ -30,6 +31,8 @@ func init() {
bus
.
AddHandler
(
"sql"
,
DeleteUser
)
bus
.
AddHandler
(
"sql"
,
UpdateUserPermissions
)
bus
.
AddHandler
(
"sql"
,
SetUserHelpFlag
)
bus
.
AddCtxHandler
(
"sql"
,
CreateUserCtx
)
}
func
getOrgIdForNewUser
(
cmd
*
m
.
CreateUserCommand
,
sess
*
DBSession
)
(
int64
,
error
)
{
...
...
@@ -79,77 +82,87 @@ func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error
return
org
.
Id
,
nil
}
func
CreateUser
(
cmd
*
m
.
CreateUserCommand
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
orgId
,
err
:=
getOrgIdForNewUser
(
cmd
,
sess
)
if
err
!=
nil
{
return
err
}
func
internalCreateUser
(
sess
*
DBSession
,
cmd
*
m
.
CreateUserCommand
)
error
{
orgId
,
err
:=
getOrgIdForNewUser
(
cmd
,
sess
)
if
err
!=
nil
{
return
err
}
if
cmd
.
Email
==
""
{
cmd
.
Email
=
cmd
.
Login
}
if
cmd
.
Email
==
""
{
cmd
.
Email
=
cmd
.
Login
}
// create user
user
:=
m
.
User
{
Email
:
cmd
.
Email
,
Name
:
cmd
.
Name
,
Login
:
cmd
.
Login
,
Company
:
cmd
.
Company
,
IsAdmin
:
cmd
.
IsAdmin
,
OrgId
:
orgId
,
EmailVerified
:
cmd
.
EmailVerified
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
LastSeenAt
:
time
.
Now
()
.
AddDate
(
-
10
,
0
,
0
),
}
// create user
user
:=
m
.
User
{
Email
:
cmd
.
Email
,
Name
:
cmd
.
Name
,
Login
:
cmd
.
Login
,
Company
:
cmd
.
Company
,
IsAdmin
:
cmd
.
IsAdmin
,
OrgId
:
orgId
,
EmailVerified
:
cmd
.
EmailVerified
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
LastSeenAt
:
time
.
Now
()
.
AddDate
(
-
10
,
0
,
0
),
}
if
len
(
cmd
.
Password
)
>
0
{
user
.
Salt
=
util
.
GetRandomString
(
10
)
user
.
Rands
=
util
.
GetRandomString
(
10
)
user
.
Password
=
util
.
EncodePassword
(
cmd
.
Password
,
user
.
Salt
)
}
if
len
(
cmd
.
Password
)
>
0
{
user
.
Salt
=
util
.
GetRandomString
(
10
)
user
.
Rands
=
util
.
GetRandomString
(
10
)
user
.
Password
=
util
.
EncodePassword
(
cmd
.
Password
,
user
.
Salt
)
}
sess
.
UseBool
(
"is_admin"
)
sess
.
UseBool
(
"is_admin"
)
if
_
,
err
:=
sess
.
Insert
(
&
user
);
err
!=
nil
{
return
err
}
if
_
,
err
:=
sess
.
Insert
(
&
user
);
err
!=
nil
{
return
err
}
sess
.
publishAfterCommit
(
&
events
.
UserCreated
{
Timestamp
:
user
.
Created
,
Id
:
user
.
Id
,
Name
:
user
.
Name
,
Login
:
user
.
Login
,
Email
:
user
.
Email
,
})
sess
.
publishAfterCommit
(
&
events
.
UserCreated
{
Timestamp
:
user
.
Created
,
Id
:
user
.
Id
,
Name
:
user
.
Name
,
Login
:
user
.
Login
,
Email
:
user
.
Email
,
})
cmd
.
Result
=
user
cmd
.
Result
=
user
// create org user link
if
!
cmd
.
SkipOrgSetup
{
orgUser
:=
m
.
OrgUser
{
OrgId
:
orgId
,
UserId
:
user
.
Id
,
Role
:
m
.
ROLE_ADMIN
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
// create org user link
if
!
cmd
.
SkipOrgSetup
{
orgUser
:=
m
.
OrgUser
{
OrgId
:
orgId
,
UserId
:
user
.
Id
,
Role
:
m
.
ROLE_ADMIN
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
if
setting
.
AutoAssignOrg
&&
!
user
.
IsAdmin
{
if
len
(
cmd
.
DefaultOrgRole
)
>
0
{
orgUser
.
Role
=
m
.
RoleType
(
cmd
.
DefaultOrgRole
)
}
else
{
orgUser
.
Role
=
m
.
RoleType
(
setting
.
AutoAssignOrgRole
)
}
if
setting
.
AutoAssignOrg
&&
!
user
.
IsAdmin
{
if
len
(
cmd
.
DefaultOrgRole
)
>
0
{
orgUser
.
Role
=
m
.
RoleType
(
cmd
.
DefaultOrgRole
)
}
else
{
orgUser
.
Role
=
m
.
RoleType
(
setting
.
AutoAssignOrgRole
)
}
}
if
_
,
err
=
sess
.
Insert
(
&
orgUser
);
err
!=
nil
{
return
err
}
if
_
,
err
=
sess
.
Insert
(
&
orgUser
);
err
!=
nil
{
return
err
}
}
return
nil
return
nil
}
func
CreateUserCtx
(
ctx
context
.
Context
,
cmd
*
m
.
CreateUserCommand
)
error
{
return
inTransactionWithRetryCtx
(
ctx
,
func
(
sess
*
DBSession
)
error
{
return
internalCreateUser
(
sess
,
cmd
)
},
0
)
}
func
CreateUser
(
cmd
*
m
.
CreateUserCommand
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
return
internalCreateUser
(
sess
,
cmd
)
})
}
...
...
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