forked from ory/hydra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
warden: add group management - closes #68
- Loading branch information
Showing
24 changed files
with
1,039 additions
and
39 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,128 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ory-am/hydra/config" | ||
"github.com/ory-am/hydra/pkg" | ||
"github.com/ory-am/hydra/warden/group" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type GroupHandler struct { | ||
Config *config.Config | ||
M *group.HTTPManager | ||
} | ||
|
||
func newGroupHandler(c *config.Config) *GroupHandler { | ||
return &GroupHandler{ | ||
Config: c, | ||
M: &group.HTTPManager{}, | ||
} | ||
} | ||
|
||
func (h *GroupHandler) CreateGroup(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
fmt.Print(cmd.UsageString()) | ||
return | ||
} | ||
|
||
var err error | ||
h.M.Dry, _ = cmd.Flags().GetBool("dry") | ||
h.M.Endpoint = h.Config.Resolve("/warden/groups") | ||
h.M.Client = h.Config.OAuth2Client(cmd) | ||
|
||
cc := &group.Group{ID: args[0]} | ||
err = h.M.CreateGroup(cc) | ||
if h.M.Dry { | ||
fmt.Printf("%s\n", err) | ||
return | ||
} | ||
|
||
pkg.Must(err, "Could not create group: %s", err) | ||
fmt.Printf("Group %s created.\n", cc.ID) | ||
} | ||
|
||
func (h *GroupHandler) DeleteGroup(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
fmt.Print(cmd.UsageString()) | ||
return | ||
} | ||
|
||
var err error | ||
h.M.Dry, _ = cmd.Flags().GetBool("dry") | ||
h.M.Endpoint = h.Config.Resolve("/warden/groups") | ||
h.M.Client = h.Config.OAuth2Client(cmd) | ||
|
||
cc := &group.Group{ID: args[0]} | ||
err = h.M.CreateGroup(cc) | ||
if h.M.Dry { | ||
fmt.Printf("%s\n", err) | ||
return | ||
} | ||
|
||
pkg.Must(err, "Could not create group: %s", err) | ||
fmt.Printf("Group %s deleted.\n", cc.ID) | ||
} | ||
|
||
func (h *GroupHandler) AddMembers(cmd *cobra.Command, args []string) { | ||
if len(args) < 2 { | ||
fmt.Print(cmd.UsageString()) | ||
return | ||
} | ||
|
||
var err error | ||
h.M.Dry, _ = cmd.Flags().GetBool("dry") | ||
h.M.Endpoint = h.Config.Resolve("/warden/groups") | ||
h.M.Client = h.Config.OAuth2Client(cmd) | ||
|
||
err = h.M.AddGroupMembers(args[0], args[1:]) | ||
if h.M.Dry { | ||
fmt.Printf("%s\n", err) | ||
return | ||
} | ||
|
||
pkg.Must(err, "Could not add members to group: %s", err) | ||
fmt.Printf("Members %v added to group %s.\n", args[1:], args[0]) | ||
} | ||
|
||
func (h *GroupHandler) RemoveMembers(cmd *cobra.Command, args []string) { | ||
if len(args) < 2 { | ||
fmt.Print(cmd.UsageString()) | ||
return | ||
} | ||
|
||
var err error | ||
h.M.Dry, _ = cmd.Flags().GetBool("dry") | ||
h.M.Endpoint = h.Config.Resolve("/warden/groups") | ||
h.M.Client = h.Config.OAuth2Client(cmd) | ||
|
||
err = h.M.RemoveGroupMembers(args[0], args[1:]) | ||
if h.M.Dry { | ||
fmt.Printf("%s\n", err) | ||
return | ||
} | ||
|
||
pkg.Must(err, "Could not remove members to group: %s", err) | ||
fmt.Printf("Members %v removed from group %s.\n", args[1:], args[0]) | ||
} | ||
|
||
func (h *GroupHandler) FindGroups(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
fmt.Print(cmd.UsageString()) | ||
return | ||
} | ||
|
||
h.M.Dry, _ = cmd.Flags().GetBool("dry") | ||
h.M.Endpoint = h.Config.Resolve("/warden/groups") | ||
h.M.Client = h.Config.OAuth2Client(cmd) | ||
|
||
gn, err := h.M.FindGroupNames(args[0]) | ||
if h.M.Dry { | ||
fmt.Printf("%s\n", err) | ||
return | ||
} | ||
|
||
pkg.Must(err, "Could not find groups: %s", err) | ||
fmt.Printf("Subject %s belongs to groups %v.\n", args[0], gn) | ||
} |
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,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// groupsCmd represents the groups command | ||
var groupsCmd = &cobra.Command{ | ||
Use: "groups", | ||
Short: "Manage warden groups", | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(groupsCmd) | ||
} |
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,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// createCmd represents the create command | ||
var createCmd = &cobra.Command{ | ||
Use: "create <id>", | ||
Short: "Create a warden group", | ||
Long: `This command creates a warden group. | ||
Example: | ||
hydra groups create my-group | ||
`, | ||
Run: cmdHandler.Groups.CreateGroup, | ||
} | ||
|
||
func init() { | ||
groupsCmd.AddCommand(createCmd) | ||
} |
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,22 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// deleteCmd represents the delete command | ||
var deleteCmd = &cobra.Command{ | ||
Use: "delete <id>", | ||
Short: "Delete a warden group", | ||
Long: `This command deletes a warden group. | ||
Example: | ||
hydra groups delete my-group | ||
`, | ||
Run: cmdHandler.Groups.DeleteGroup, | ||
} | ||
|
||
func init() { | ||
groupsCmd.AddCommand(deleteCmd) | ||
|
||
} |
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,21 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// findCmd represents the find command | ||
var findCmd = &cobra.Command{ | ||
Use: "find <subject>", | ||
Short: "Find all groups a subject belongs to", | ||
Long: `This command find all groups a subject belongs to. | ||
Example: | ||
hydra groups find peter | ||
`, | ||
Run: cmdHandler.Groups.FindGroups, | ||
} | ||
|
||
func init() { | ||
groupsCmd.AddCommand(findCmd) | ||
} |
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,14 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var groupsMembersCmd = &cobra.Command{ | ||
Use: "members", | ||
Short: "Manage warden group members", | ||
} | ||
|
||
func init() { | ||
groupsCmd.AddCommand(groupsMembersCmd) | ||
} |
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,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var addCmd = &cobra.Command{ | ||
Use: "add <group> <member> [<member>...]", | ||
Short: "Add members to a warden group", | ||
Long: `This command adds members to a warden group. | ||
Example: | ||
hydra groups members add my-group peter julia | ||
`, | ||
Run: cmdHandler.Groups.AddMembers, | ||
} | ||
|
||
func init() { | ||
groupsMembersCmd.AddCommand(addCmd) | ||
} |
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,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var removeCmd = &cobra.Command{ | ||
Use: "remove <group> <member> [<member>...]", | ||
Short: "Remove members from a warden group", | ||
Long: `This command removes members from a warden group. | ||
Example: | ||
hydra groups members remove my-group peter julia | ||
`, | ||
Run: cmdHandler.Groups.RemoveMembers, | ||
} | ||
|
||
func init() { | ||
groupsMembersCmd.AddCommand(removeCmd) | ||
} |
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
Oops, something went wrong.