Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Add user groups #1228

Merged
merged 76 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
ef462ee
adding group
mteodor Aug 21, 2020
59c1b15
adding user group
mteodor Aug 21, 2020
056f02a
adding group
mteodor Aug 21, 2020
1dee666
add groups
mteodor Aug 26, 2020
de67a01
add groups
mteodor Aug 26, 2020
5927f8b
add retrieve methods
mteodor Aug 27, 2020
3011c46
add default admin user
mteodor Aug 27, 2020
25b5352
add default admin user
mteodor Aug 28, 2020
c4e3d00
adding endpoints
mteodor Aug 31, 2020
ab94a31
adding endpoints
mteodor Aug 31, 2020
d7ee466
adding tests
mteodor Sep 7, 2020
3f5dad2
changes signature for AssignUser
mteodor Sep 7, 2020
29167c3
adding tests
mteodor Sep 7, 2020
a6d217e
bug fixing retrieving groups
mteodor Sep 10, 2020
b3aff9c
remove unused code
mteodor Sep 10, 2020
68beace
bug fixing retrieving groups
mteodor Sep 10, 2020
ca5f7e8
retrieve groups
mteodor Sep 10, 2020
4bb7865
change environment for admin
mteodor Sep 10, 2020
be0b90d
change environment for admin
mteodor Sep 10, 2020
f64f387
retrieve groups
mteodor Sep 10, 2020
c901485
remove adding default group
mteodor Sep 10, 2020
63ead05
expose port for debugging purposes
mteodor Sep 10, 2020
cc18524
fix tests, and linter errors
mteodor Sep 11, 2020
936a32f
add prefix Users for groups endpoint
mteodor Sep 11, 2020
23b5dd7
fix linter problems
mteodor Sep 11, 2020
0a24e7f
fix endpoint prefix url
mteodor Sep 11, 2020
a755af4
fix endpoint test
mteodor Sep 12, 2020
e018593
add group features in cli
mteodor Sep 14, 2020
cc3d35b
remove comments
mteodor Sep 14, 2020
5bc4828
remove println
mteodor Sep 14, 2020
9246672
when user is created return id in response
mteodor Sep 14, 2020
db8221f
when user is created return id in response
mteodor Sep 14, 2020
1a8711c
adding default admin env
mteodor Sep 14, 2020
8a66eed
proper alignment
mteodor Sep 15, 2020
d6c482a
proper alignment
mteodor Sep 15, 2020
b1669d1
fix comments
mteodor Sep 15, 2020
a49ea50
rename method
mteodor Sep 15, 2020
2c1a9ac
return user id when created
mteodor Sep 15, 2020
6ea0c5e
return user id when created
mteodor Sep 15, 2020
267247b
remove unused variable
mteodor Sep 15, 2020
8e7beac
rename methods
mteodor Sep 15, 2020
c21fc2b
fix to retrieve whole tree starting from parent
mteodor Sep 15, 2020
d5482bf
add endpoint to list groups for user
mteodor Sep 15, 2020
d7b27ca
add readme for groups
mteodor Sep 15, 2020
f69ef82
fixing bugs
mteodor Sep 15, 2020
3cca7c0
fixing bugs
mteodor Sep 15, 2020
4722eb0
add group commands for add and remove user
mteodor Sep 15, 2020
28d4992
replace default email, use example.com
mteodor Sep 16, 2020
12416dc
fix capital letters beginning of sentence
mteodor Sep 16, 2020
1621e76
remove warning for deprecated api, mistakenly copied
mteodor Sep 16, 2020
beb9344
simplify repo methods, rely on db driver rather than the check before…
mteodor Sep 16, 2020
adbdddd
check if group is valid
mteodor Sep 16, 2020
d35dfb3
openapi spec 3.0
mteodor Sep 18, 2020
53c4168
remove check for existing users in groups before delete
mteodor Sep 18, 2020
a00b38d
renaming methods
mteodor Sep 18, 2020
0b036e3
renaming methods
mteodor Sep 18, 2020
d557160
renaming methods
mteodor Sep 18, 2020
a230292
change func signature
mteodor Sep 18, 2020
8643520
change func signature
mteodor Sep 18, 2020
531fd83
fix bugs, resolve comments
mteodor Sep 21, 2020
9773d2b
fix bugs, resolve comments
mteodor Sep 21, 2020
771c0d8
fix alignment
mteodor Sep 21, 2020
a317ce4
add missing command
mteodor Sep 21, 2020
9d8bb65
reorganize envs
mteodor Sep 21, 2020
8f90d22
fix doc
mteodor Sep 21, 2020
76f061e
fix compile
mteodor Sep 22, 2020
28f7d41
reorganize cli commands
mteodor Sep 22, 2020
f49e35d
minor corrections
mteodor Sep 22, 2020
063e165
renaming
mteodor Sep 22, 2020
b6caf7b
renaming
mteodor Sep 22, 2020
0fb9195
renaming
mteodor Sep 22, 2020
2e1136b
rename methods
mteodor Sep 23, 2020
9f6158b
fix naming
mteodor Sep 23, 2020
d6cc349
renaming
mteodor Sep 23, 2020
7f685b7
renaming
mteodor Sep 23, 2020
b12dcbb
resolve comments, minor changes
mteodor Sep 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ MF_USERS_DB_PORT=5432
MF_USERS_DB_USER=mainflux
MF_USERS_DB_PASS=mainflux
MF_USERS_DB=users
MF_USERS_ADMIN_EMAIL=admin@example.com
MF_USERS_ADMIN_PASSWORD=12345678

### Email utility
MF_EMAIL_DRIVER=smtp
Expand Down
38 changes: 38 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,41 @@ mainflux-cli bootstrap remove <thing_id> <user_auth_token>
```bash
mainflux-cli bootstrap bootstrap <external_id> <external_key>
```

### Groups
#### Create new group
```bash
mainflux-cli groups create '{"name":"<group_name>","parent_id":"<parent_group_id>","description":"<description>","metadata":{"key":"value",...}}' <user_auth_token>
```
#### Delete group
```bash
mainflux-cli groups delete <group_id> <user_auth_token>
```
#### Get group with id
```bash
mainflux-cli groups get <group_id> <user_auth_token>
```
#### List all groups
```bash
mainflux-cli groups get all <user_auth_token>
```
#### List children groups for some group
```bash
mainflux-cli groups get children <parent_group_id> <user_auth_token>
```
#### Assign user to a group
```bash
mainflux-cli groups assign <user_id> <group_id> <user_auth_token>
```
#### Unassign user from group
```bash
mainflux-cli groups unassign <user_id> <group_id> <user_auth_token>
```
#### List users for a group
```bash
mainflux-cli groups members <group_id> <user_auth_token>
```
#### List groups that user belongs to
```bash
mainflux-cli groups membership <user_id> <user_auth_token>
```
182 changes: 182 additions & 0 deletions cli/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

package cli

import (
"encoding/json"

mfxsdk "github.com/mainflux/mainflux/pkg/sdk/go"
"github.com/spf13/cobra"
)

var cmdGroups = []cobra.Command{
cobra.Command{
Use: "create",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the full command? group create ...? Would it not be confusing when we add other entities groups (things, channels)?

Short: "create <JSON_group> <user_auth_token>",
Long: `Creates new group
JSON_group:
{
"Name":<group_name>,
"Description":<description>,
"ParentID":<parent_id>,
"Metadata":<metadata>,
}
Name - is unique group name
ParentID - ID of a group that is a parent to the creating group
Metadata - JSON structured string`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Short)
return
}
var group mfxsdk.Group
if err := json.Unmarshal([]byte(args[0]), &group); err != nil {
logError(err)
return
}
id, err := sdk.CreateGroup(group, args[1])
if err != nil {
logError(err)
return
}
logCreated(id)
},
},
cobra.Command{
Use: "get",
Short: "get [all | children <group_id> | group_id] <user_auth_token>",
Long: `Get all users groups, group children or group by id.
all - lists all groups
children <group_id> - lists all children groups of <group_id>
<group_id> - shows group with provided group ID`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
logUsage(cmd.Short)
return
}
if args[0] == "all" {
l, err := sdk.Groups(args[1], uint64(Offset), uint64(Limit), "")
if err != nil {
logError(err)
return
}
logJSON(l)
return
}
if args[0] == "children" {
l, err := sdk.Groups(args[2], uint64(Offset), uint64(Limit), args[1])
if err != nil {
logError(err)
return
}
logJSON(l)
return
}
t, err := sdk.Group(args[0], args[1])
if err != nil {
logError(err)
return
}
logJSON(t)
},
},
cobra.Command{
Use: "assign",
Short: "assign <user_id> <group_id> <user_auth_token>",
Long: `Assign user to a group.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
logUsage(cmd.Short)
return
}
if err := sdk.Assign(args[0], args[1], args[2]); err != nil {
logError(err)
return
}
logOK()
},
},
cobra.Command{
Use: "unassign",
Short: "unassign <user_id> <group_id> <user_auth_token>",
Long: `Unassign user from a group.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 3 {
logUsage(cmd.Short)
return
}
if err := sdk.Unassign(args[0], args[1], args[2]); err != nil {
logError(err)
return
}
logOK()
},
},
cobra.Command{
Use: "delete",
Short: "delete <group_id> <user_auth_token>",
Long: `Delete users group.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Short)
return
}
if err := sdk.DeleteGroup(args[0], args[1]); err != nil {
logError(err)
return
}
logOK()
},
},
cobra.Command{
Use: "members",
Short: "members <group_id> <user_auth_token>",
Long: `Lists all user members of a group.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Short)
return
}
up, err := sdk.Members(args[0], args[1], uint64(Offset), uint64(Limit))
if err != nil {
logError(err)
return
}
logJSON(up)
},
},
cobra.Command{
Use: "membership",
Short: "membership <user_id> <user_auth_token>",
Comment on lines +150 to +151
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memberships

Long: `List user groups membership`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Short)
return
}
up, err := sdk.Memberships(args[0], args[1], uint64(Offset), uint64(Limit))
if err != nil {
logError(err)
return
}
logJSON(up)
},
},
}

// NewGroupsCmd returns users command.
func NewGroupsCmd() *cobra.Command {
cmd := cobra.Command{
Use: "groups",
Short: "Groups management",
Long: `Groups management: create groups and assigns user to groups"`,
Run: func(cmd *cobra.Command, args []string) {
logUsage("Usage: Groups [create | get | delete | assign | unassign | members | membership]")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you change previous one be careful here

},
}
for i := range cmdGroups {
cmd.AddCommand(&cmdGroups[i])
}
return &cmd
}
2 changes: 1 addition & 1 deletion cli/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var cmdProvision = []cobra.Command{
Email: un,
Password: "12345678",
}
if err := sdk.CreateUser(user); err != nil {
if _, err := sdk.CreateUser(user); err != nil {
logError(err)
return
}
Expand Down
5 changes: 3 additions & 2 deletions cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ var cmdUsers = []cobra.Command{
Email: args[0],
Password: args[1],
}
if err := sdk.CreateUser(user); err != nil {
id, err := sdk.CreateUser(user)
if err != nil {
logError(err)
return
}

logOK()
logCreated(id)
},
},
cobra.Command{
Expand Down
11 changes: 11 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
CertsURL: "http://localhost:8204",
ReaderPrefix: "",
UsersPrefix: "",
GroupsPrefix: "",
ThingsPrefix: "",
HTTPAdapterPrefix: "http",
BootstrapPrefix: "things",
Expand All @@ -41,6 +42,7 @@ func main() {
versionCmd := cli.NewVersionCmd()
usersCmd := cli.NewUsersCmd()
thingsCmd := cli.NewThingsCmd()
groupsCmd := cli.NewGroupsCmd()
channelsCmd := cli.NewChannelsCmd()
messagesCmd := cli.NewMessagesCmd()
provisionCmd := cli.NewProvisionCmd()
Expand All @@ -50,6 +52,7 @@ func main() {
// Root Commands
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(usersCmd)
rootCmd.AddCommand(groupsCmd)
rootCmd.AddCommand(thingsCmd)
rootCmd.AddCommand(channelsCmd)
rootCmd.AddCommand(messagesCmd)
Expand Down Expand Up @@ -82,6 +85,14 @@ func main() {
"Mainflux things service prefix",
)

rootCmd.PersistentFlags().StringVarP(
&sdkConf.GroupsPrefix,
"groups-prefix",
"g",
sdkConf.GroupsPrefix,
"Mainflux groups service prefix",
)

rootCmd.PersistentFlags().StringVarP(
&sdkConf.HTTPAdapterPrefix,
"http-prefix",
Expand Down
2 changes: 1 addition & 1 deletion cmd/provision/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func main() {
// Merge environment variables and file settings.
mergeConfigs(&cfgFromFile, &cfg)
cfg = cfgFromFile
logger.Info("Continue with settings from file:" + cfg.File)
logger.Info("Continue with settings from file: " + cfg.File)
}

SDKCfg := mfSDK.Config{
Expand Down
Loading