-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [#440] Create user and assign roles.
- Loading branch information
Showing
11 changed files
with
518 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/030/n3dr/internal/app/n3dr/config/user" | ||
"github.com/030/n3dr/internal/app/n3dr/connection" | ||
"github.com/030/n3dr/internal/app/n3dr/goswagger/models" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var downloadRole, uploadRole bool | ||
|
||
// configUserCmd represents the configUser command. | ||
var configRoleCmd = &cobra.Command{ | ||
Use: "configRole", | ||
Short: "Configure roles.", | ||
Long: `Create roles. | ||
Examples: | ||
# Create a download role: | ||
n3dr configRole --downloadRole | ||
# Create an upload role: | ||
n3dr configRole --uploadRole | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if !downloadRole && !uploadRole { | ||
log.Fatal("either the downloadRole or uploadRole is required") | ||
} | ||
|
||
acu := models.APICreateUser{ | ||
EmailAddress: email, | ||
FirstName: firstName, | ||
LastName: lastName, | ||
Password: pass, | ||
UserID: id, | ||
} | ||
n := connection.Nexus3{ | ||
FQDN: n3drURL, | ||
HTTPS: &https, | ||
Pass: n3drPass, | ||
User: n3drUser, | ||
} | ||
u := user.User{APICreateUser: acu, Nexus3: n} | ||
|
||
if downloadRole { | ||
u.Roles = []string{"nx-download"} | ||
rr := models.RoleXORequest{ | ||
ID: "nx-download", | ||
Name: "nx-download", | ||
Privileges: []string{ | ||
"nx-repository-view-*-*-browse", | ||
"nx-repository-view-*-*-read", | ||
}, | ||
} | ||
r := user.Role{RoleXORequest: rr, Nexus3: n} | ||
if err := r.CreateRole(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
if uploadRole { | ||
u.Roles = []string{"nx-upload"} | ||
rr := models.RoleXORequest{ | ||
ID: "nx-upload", | ||
Name: "nx-upload", | ||
Privileges: []string{ | ||
"nx-repository-view-*-*-add", | ||
"nx-repository-view-*-*-edit", | ||
}, | ||
} | ||
r := user.Role{RoleXORequest: rr, Nexus3: n} | ||
if err := r.CreateRole(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(configRoleCmd) | ||
|
||
configRoleCmd.Flags().BoolVar(&downloadRole, "downloadRole", false, "Whether a download role should be created") | ||
configRoleCmd.Flags().BoolVar(&uploadRole, "uploadRole", false, "Whether an upload role should be created") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,9 @@ import ( | |
) | ||
|
||
var ( | ||
admin, changePass, downloadUser, uploadUser bool | ||
email, firstName, id, lastName, pass string | ||
admin, changePass, custom, downloadUser, uploadUser bool | ||
email, firstName, id, lastName, pass string | ||
roles []string | ||
) | ||
|
||
// configUserCmd represents the configUser command. | ||
|
@@ -20,12 +21,15 @@ var configUserCmd = &cobra.Command{ | |
Long: `Create users or change their passwords. | ||
Examples: | ||
# Create a custom user and assign certain roles: | ||
n3dr configUser --pass some-pass --email [email protected] --firstName build --id build --lastName build --roles nx-download,nx-upload --custom | ||
# Change the admin password: | ||
n3dr configUser --changePass --https=false --n3drUser admin --n3drURL nexus3:8081 --n3drPass initial-pass --pass some-pass --email [email protected] --firstName admin --id admin --lastName admin | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if !admin && !downloadUser && !uploadUser && !changePass { | ||
log.Fatal("either the admin, changePass, downloadUser or uploadUser is required") | ||
if !admin && !custom && !downloadUser && !uploadUser && !changePass { | ||
log.Fatal("either the admin, custom, changePass, create, downloadUser or uploadUser is required") | ||
} | ||
|
||
acu := models.APICreateUser{ | ||
|
@@ -50,6 +54,15 @@ Examples: | |
} | ||
} | ||
|
||
if custom { | ||
u.Roles = roles | ||
log.Info("roles:", u) | ||
|
||
if err := u.Create(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
if downloadUser { | ||
u.Roles = []string{"nx-download"} | ||
rr := models.RoleXORequest{ | ||
|
@@ -125,7 +138,10 @@ func init() { | |
} | ||
|
||
configUserCmd.Flags().BoolVar(&admin, "admin", false, "Whether a user should be admin") | ||
configUserCmd.Flags().BoolVar(&custom, "custom", false, "Create a user and assign certain roles") | ||
configUserCmd.Flags().BoolVar(&downloadUser, "downloadUser", false, "Whether a user should be able to download") | ||
configUserCmd.Flags().BoolVar(&uploadUser, "uploadUser", false, "Whether a user should be able to upload") | ||
configUserCmd.Flags().BoolVar(&changePass, "changePass", false, "Whether a pass should be changed") | ||
|
||
configUserCmd.Flags().StringArrayVar(&roles, "roles", nil, "Which roles have to be assigned to the custom user") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.