Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Secure solution for nexus infrastructure
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
flg_masters
TM
Secure solution for nexus infrastructure
Commits
5e17b7ad
Commit
5e17b7ad
authored
2 years ago
by
Florent Gluck
Browse files
Options
Downloads
Patches
Plain Diff
usersetcaps now takes a csv file in argument to change user capabilities in bulk
parent
f154a60d
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/cmdUser/userAdd.go
+5
-7
5 additions, 7 deletions
src/cmdUser/userAdd.go
src/cmdUser/userDel.go
+1
-0
1 addition, 0 deletions
src/cmdUser/userDel.go
src/cmdUser/userSetCaps.go
+96
-17
96 additions, 17 deletions
src/cmdUser/userSetCaps.go
src/version/version.go
+1
-1
1 addition, 1 deletion
src/version/version.go
with
103 additions
and
25 deletions
src/cmdUser/userAdd.go
+
5
−
7
View file @
5e17b7ad
...
...
@@ -53,6 +53,7 @@ func (cmd *Add)Run(args []string) int {
argc
:=
len
(
args
)
if
argc
>=
4
{
// Detected syntax: email firstname lastname pwd [caps]
cargs
:=
&
cmdArgs
{
Email
:
args
[
0
],
FirstName
:
args
[
1
],
...
...
@@ -72,11 +73,7 @@ func (cmd *Add)Run(args []string) int {
}
}
else
if
argc
==
1
{
// Detected syntax: file.csv
// column1 email
// column2 firstname
// column3 lastname
// column4 password
// column5 capabilities (space separated)`
csvFile
:=
args
[
0
]
file
,
err
:=
os
.
Open
(
csvFile
)
if
err
!=
nil
{
...
...
@@ -110,8 +107,9 @@ func (cmd *Add)Run(args []string) int {
Pwd
:
columns
[
3
],
Caps
:
make
(
map
[
string
]
int
),
}
if
len
(
columns
[
4
])
>
0
{
caps
:=
strings
.
Split
(
columns
[
4
],
" "
)
capsStr
:=
strings
.
TrimSpace
(
columns
[
4
])
if
len
(
capsStr
)
>
0
{
caps
:=
strings
.
Split
(
capsStr
,
" "
)
for
_
,
cap
:=
range
caps
{
cargs
.
Caps
[
cap
]
=
1
}
...
...
This diff is collapsed.
Click to expand it.
src/cmdUser/userDel.go
+
1
−
0
View file @
5e17b7ad
...
...
@@ -45,6 +45,7 @@ func (cmd *Del)Run(args []string) int {
if
argc
==
1
&&
!
u
.
IsEmail
(
args
[
0
])
{
// Single argument and it's a CSV file
csvFile
:=
args
[
0
]
if
u
.
IsEmail
(
csvFile
)
{
...
...
This diff is collapsed.
Click to expand it.
src/cmdUser/userSetCaps.go
+
96
−
17
View file @
5e17b7ad
package
cmdUser
import
(
"io"
"os"
"errors"
"strings"
"encoding/csv"
u
"nexus-client/utils"
g
"nexus-client/globals"
)
...
...
@@ -25,41 +30,115 @@ func (cmd *SetCaps)PrintUsage() {
}
u
.
PrintlnErr
(
"―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――"
)
u
.
PrintlnErr
(
"USAGE: "
+
cmd
.
Name
+
" email [capability ...]"
)
u
.
PrintlnErr
(
" "
+
cmd
.
Name
+
" file.csv"
)
u
.
PrintlnErr
(
"―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――"
)
const
usage
string
=
`file.csv 2-column CSV file defining each user's capabilities:
column1 email
column2 capabilities (space separated)`
u
.
PrintlnErr
(
usage
)
}
func
(
cmd
*
SetCaps
)
Run
(
a
rgs
[]
str
ing
)
in
t
{
client
:=
g
.
GetInstance
()
.
Client
host
:=
g
.
GetInstance
()
.
Host
type
capsA
rgs
str
uc
t
{
Caps
map
[
string
]
int
`json:"caps" validate:"required"`
}
func
(
cmd
*
SetCaps
)
Run
(
args
[]
string
)
int
{
argc
:=
len
(
args
)
if
argc
<
1
{
cmd
.
PrintUsage
()
return
1
}
email
:=
args
[
0
]
statusCode
:=
0
type
CapsArgs
struct
{
Caps
map
[
string
]
int
`json:"caps" validate:"required"`
}
if
argc
==
1
&&
!
u
.
IsEmail
(
args
[
0
])
{
// Single argument and it's a CSV file
capsArgs
:=
&
CapsArgs
{
make
(
map
[
string
]
int
)
}
for
_
,
cap
:=
range
args
[
1
:
]
{
capsArgs
.
Caps
[
cap
]
=
1
csvFile
:=
args
[
0
]
if
u
.
IsEmail
(
csvFile
)
{
cmd
.
PrintUsage
()
return
1
}
resp
,
err
:=
client
.
R
()
.
SetBody
(
capsArgs
)
.
Put
(
host
+
"/users/"
+
email
+
"/caps"
)
file
,
err
:=
os
.
Open
(
csvFile
)
if
err
!=
nil
{
u
.
PrintlnErr
(
"Error: "
+
err
.
Error
())
return
1
}
defer
file
.
Close
()
reader
:=
csv
.
NewReader
(
file
)
line
:=
0
for
{
line
+=
1
columns
,
err
:=
reader
.
Read
()
if
err
==
io
.
EOF
{
break
}
if
err
!=
nil
{
u
.
PrintlnErr
(
"FAILED reading "
+
err
.
Error
())
statusCode
=
1
continue
}
columnCount
:=
len
(
columns
)
if
columnCount
!=
2
{
u
.
PrintlnErr
(
"FAILED reading record on line "
,
line
,
": expecting 2 columns"
)
statusCode
=
1
continue
}
userCaps
:=
&
capsArgs
{
make
(
map
[
string
]
int
)
}
capsStr
:=
strings
.
TrimSpace
(
columns
[
1
])
if
len
(
capsStr
)
>
0
{
caps
:=
strings
.
Split
(
capsStr
,
" "
)
for
_
,
cap
:=
range
caps
{
userCaps
.
Caps
[
cap
]
=
1
}
}
email
:=
columns
[
0
]
if
err
:=
cmd
.
runRequest
(
email
,
userCaps
);
err
!=
nil
{
u
.
PrintlnErr
(
err
.
Error
())
statusCode
=
1
}
else
{
u
.
Println
(
"Successfully set capabilities for "
+
email
)
}
}
}
else
{
// Argument is an email address
email
:=
args
[
0
]
userCaps
:=
&
capsArgs
{
make
(
map
[
string
]
int
)
}
for
_
,
cap
:=
range
args
[
1
:
]
{
userCaps
.
Caps
[
cap
]
=
1
}
if
err
:=
cmd
.
runRequest
(
email
,
userCaps
);
err
!=
nil
{
u
.
PrintlnErr
(
err
.
Error
())
statusCode
=
1
}
else
{
u
.
Println
(
"Successfully set capabilities for "
+
email
)
}
}
return
statusCode
}
func
(
cmd
*
SetCaps
)
runRequest
(
email
string
,
userCaps
*
capsArgs
)
error
{
client
:=
g
.
GetInstance
()
.
Client
host
:=
g
.
GetInstance
()
.
Host
resp
,
err
:=
client
.
R
()
.
SetBody
(
userCaps
)
.
Put
(
host
+
"/users/"
+
email
+
"/caps"
)
if
err
!=
nil
{
return
errors
.
New
(
"Error: "
+
err
.
Error
())
}
if
resp
.
IsSuccess
()
{
u
.
Println
(
resp
)
return
0
return
nil
}
else
{
u
.
PrintlnErr
(
"Error: "
+
resp
.
Status
()
+
": "
+
resp
.
String
())
return
1
return
errors
.
New
(
"Error: "
+
resp
.
Status
()
+
": "
+
resp
.
String
())
}
}
This diff is collapsed.
Click to expand it.
src/version/version.go
+
1
−
1
View file @
5e17b7ad
...
...
@@ -8,7 +8,7 @@ import (
const
(
major
=
1
minor
=
4
bugfix
=
1
bugfix
=
2
)
type
Version
struct
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment