2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-08-16 13:41:27 +02:00
|
|
|
package cli
|
2018-07-11 13:53:37 +02:00
|
|
|
|
|
|
|
import (
|
2020-03-04 18:37:41 +01:00
|
|
|
"encoding/json"
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
mfclients "github.com/mainflux/mainflux/pkg/clients"
|
2020-06-03 15:16:19 +02:00
|
|
|
mfxsdk "github.com/mainflux/mainflux/pkg/sdk/go"
|
2018-07-11 13:53:37 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-02-11 13:22:57 +01:00
|
|
|
var cmdUsers = []cobra.Command{
|
|
|
|
{
|
2023-06-14 12:40:37 +02:00
|
|
|
Use: "create <name> <username> <password> <user_auth_token>",
|
2022-02-11 13:22:57 +01:00
|
|
|
Short: "Create user",
|
2023-06-14 12:40:37 +02:00
|
|
|
Long: "Create user with provided name, username and password. Token in optional\n" +
|
|
|
|
"For example:\n" +
|
|
|
|
"\tmainflux-cli users create user user@example.com 12345678 $USER_AUTH_TOKEN\n",
|
2018-07-11 13:53:37 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-06-14 12:40:37 +02:00
|
|
|
if len(args) < 3 || len(args) > 4 {
|
2022-02-11 13:22:57 +01:00
|
|
|
logUsage(cmd.Use)
|
2018-10-14 16:44:21 +02:00
|
|
|
return
|
|
|
|
}
|
2023-06-14 12:40:37 +02:00
|
|
|
if len(args) == 3 {
|
2021-11-08 13:40:27 +03:00
|
|
|
args = append(args, "")
|
|
|
|
}
|
2018-10-24 11:21:03 +02:00
|
|
|
|
|
|
|
user := mfxsdk.User{
|
2023-06-14 12:40:37 +02:00
|
|
|
Name: args[0],
|
|
|
|
Credentials: mfxsdk.Credentials{
|
|
|
|
Identity: args[1],
|
|
|
|
Secret: args[2],
|
|
|
|
},
|
|
|
|
Status: mfclients.EnabledStatus.String(),
|
2018-10-24 11:21:03 +02:00
|
|
|
}
|
2023-06-14 12:40:37 +02:00
|
|
|
user, err := sdk.CreateUser(user, args[3])
|
2020-09-23 23:18:53 +02:00
|
|
|
if err != nil {
|
2018-10-14 16:44:21 +02:00
|
|
|
logError(err)
|
2018-07-11 13:53:37 +02:00
|
|
|
return
|
|
|
|
}
|
2018-10-24 11:21:03 +02:00
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logJSON(user)
|
2018-07-11 13:53:37 +02:00
|
|
|
},
|
2022-02-11 13:22:57 +01:00
|
|
|
},
|
|
|
|
{
|
2022-06-17 18:32:23 +03:00
|
|
|
Use: "get [all | <user_id> ] <user_auth_token>",
|
|
|
|
Short: "Get users",
|
2023-06-14 12:40:37 +02:00
|
|
|
Long: "Get all users or get user by id. Users can be filtered by name or metadata or status\n" +
|
|
|
|
"Usage:\n" +
|
|
|
|
"\tmainflux-cli users get all <user_auth_token> - lists all users\n" +
|
|
|
|
"\tmainflux-cli users get all <user_auth_token> --offset <offset> --limit <limit> - lists all users with provided offset and limit\n" +
|
|
|
|
"\tmainflux-cli users get <user_id> <user_auth_token> - shows user with provided <user_id>\n",
|
2020-03-04 18:37:41 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-06-17 18:32:23 +03:00
|
|
|
if len(args) != 2 {
|
2022-02-11 13:22:57 +01:00
|
|
|
logUsage(cmd.Use)
|
2020-03-04 18:37:41 +01:00
|
|
|
return
|
|
|
|
}
|
2022-06-17 18:32:23 +03:00
|
|
|
metadata, err := convertMetadata(Metadata)
|
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pageMetadata := mfxsdk.PageMetadata{
|
|
|
|
Email: "",
|
|
|
|
Offset: uint64(Offset),
|
|
|
|
Limit: uint64(Limit),
|
|
|
|
Metadata: metadata,
|
2022-08-11 19:58:45 +03:00
|
|
|
Status: Status,
|
2022-06-17 18:32:23 +03:00
|
|
|
}
|
MF-1718 - Use static code analysis in CI (#1729)
* things, twins, and logger lint fixed
Signed-off-by: aryan <aryangodara03@gmail.com>
* all services updated, auth jwt not working, ineffectual assignment issue
Signed-off-by: aryan <aryangodara03@gmail.com>
* handle error from grpc server in endpointtest
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, auth/jwt needs to be resolved
Signed-off-by: aryan <aryangodara03@gmail.com>
* revert back to jwt v4 temporarily
Signed-off-by: aryan <aryangodara03@gmail.com>
* updated jwt tokenizer
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve EOF error for httptest requests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix auth jwt, update to registeredclaims
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix ineffective assignment, auth/api/grpc endpoint failing
Signed-off-by: aryan <aryangodara03@gmail.com>
* temp commit, remove later
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server setup
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve golangci tests, remove debug statements
Signed-off-by: aryan <aryangodara03@gmail.com>
* update golangci version and modify linters used
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix failing tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc server for setup tests
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix logging and errors inlined
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix remarks, update grpc setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix setup_test
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix data race
Signed-off-by: aryan <aryangodara03@gmail.com>
* update setup_test grpc
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix grpc setup down to single simple function
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix linting issues
Signed-off-by: aryan <aryangodara03@gmail.com>
* resolve pr comments
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix tests, handle returned errors, go mod tidy vendor
Signed-off-by: aryan <aryangodara03@gmail.com>
* fix errors from new linters
Signed-off-by: aryan <aryangodara03@gmail.com>
---------
Signed-off-by: aryan <aryangodara03@gmail.com>
2023-04-22 08:14:35 -07:00
|
|
|
if args[0] == all {
|
2023-01-05 19:20:05 +03:00
|
|
|
l, err := sdk.Users(pageMetadata, args[1])
|
2022-06-17 18:32:23 +03:00
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logJSON(l)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u, err := sdk.User(args[0], args[1])
|
2020-03-04 18:37:41 +01:00
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logJSON(u)
|
|
|
|
},
|
2022-02-11 13:22:57 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Use: "token <username> <password>",
|
|
|
|
Short: "Get token",
|
2023-06-14 12:40:37 +02:00
|
|
|
Long: "Generate new token from username and password\n" +
|
|
|
|
"For example:\n" +
|
|
|
|
"\tmainflux-cli users token user@example.com 12345678\n",
|
2018-07-11 13:53:37 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 2 {
|
2022-02-11 13:22:57 +01:00
|
|
|
logUsage(cmd.Use)
|
2018-10-14 16:44:21 +02:00
|
|
|
return
|
|
|
|
}
|
2018-10-24 11:21:03 +02:00
|
|
|
|
|
|
|
user := mfxsdk.User{
|
2023-06-14 12:40:37 +02:00
|
|
|
Credentials: mfxsdk.Credentials{
|
|
|
|
Identity: args[0],
|
|
|
|
Secret: args[1],
|
|
|
|
},
|
2018-10-24 11:21:03 +02:00
|
|
|
}
|
|
|
|
token, err := sdk.CreateToken(user)
|
2018-10-14 16:44:21 +02:00
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
2018-07-11 13:53:37 +02:00
|
|
|
return
|
|
|
|
}
|
2018-10-24 11:21:03 +02:00
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logJSON(token)
|
2020-10-31 23:29:06 +00:00
|
|
|
|
2018-07-11 13:53:37 +02:00
|
|
|
},
|
2022-02-11 13:22:57 +01:00
|
|
|
},
|
|
|
|
{
|
2023-06-14 12:40:37 +02:00
|
|
|
Use: "refreshtoken <token>",
|
|
|
|
Short: "Get token",
|
|
|
|
Long: "Generate new token from refresh token\n" +
|
|
|
|
"For example:\n" +
|
|
|
|
"\tmainflux-cli users refreshtoken <refresh_token>\n",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
logUsage(cmd.Use)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := sdk.RefreshToken(args[0])
|
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logJSON(token)
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Use: "update [<user_id> <JSON_string> | tags <user_id> <tags> | identity <user_id> <identity> | owner <user_id> <owner>] <user_auth_token>",
|
2022-02-11 13:22:57 +01:00
|
|
|
Short: "Update user",
|
2023-06-14 12:40:37 +02:00
|
|
|
Long: "Updates either user name and metadata or user tags or user identity or user owner\n" +
|
|
|
|
"Usage:\n" +
|
|
|
|
"\tmainflux-cli users update <user_id> '{\"name\":\"new name\", \"metadata\":{\"key\": \"value\"}}' $USERTOKEN - updates user name and metadata\n" +
|
|
|
|
"\tmainflux-cli users update tags <user_id> '[\"tag1\", \"tag2\"]' $USERTOKEN - updates user tags\n" +
|
|
|
|
"\tmainflux-cli users update identity <user_id> newidentity@example.com $USERTOKEN - updates user identity\n" +
|
|
|
|
"\tmainflux-cli users update owner <user_id> <owner_id> $USERTOKEN - updates user owner\n",
|
2020-03-04 18:37:41 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-06-14 12:40:37 +02:00
|
|
|
if len(args) != 4 && len(args) != 3 {
|
2022-02-11 13:22:57 +01:00
|
|
|
logUsage(cmd.Use)
|
2020-03-04 18:37:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var user mfxsdk.User
|
2023-06-14 12:40:37 +02:00
|
|
|
if args[0] == "tags" {
|
|
|
|
if err := json.Unmarshal([]byte(args[2]), &user.Tags); err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.ID = args[1]
|
|
|
|
user, err := sdk.UpdateUserTags(user, args[3])
|
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logJSON(user)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[0] == "identity" {
|
|
|
|
user.ID = args[1]
|
|
|
|
user.Credentials.Identity = args[2]
|
|
|
|
user, err := sdk.UpdateUserIdentity(user, args[3])
|
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logJSON(user)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[0] == "owner" {
|
|
|
|
user.ID = args[1]
|
|
|
|
user.Owner = args[2]
|
|
|
|
user, err := sdk.UpdateUserOwner(user, args[3])
|
|
|
|
if err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logJSON(user)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(args[1]), &user); err != nil {
|
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.ID = args[0]
|
|
|
|
user, err := sdk.UpdateUser(user, args[2])
|
|
|
|
if err != nil {
|
2020-03-04 18:37:41 +01:00
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logJSON(user)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Use: "profile <user_auth_token>",
|
|
|
|
Short: "Get user profile",
|
|
|
|
Long: "Get user profile\n" +
|
|
|
|
"Usage:\n" +
|
|
|
|
"\tmainflux-cli users profile $USERTOKEN\n",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
logUsage(cmd.Use)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := sdk.UserProfile(args[0])
|
|
|
|
if err != nil {
|
2020-03-04 18:37:41 +01:00
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logJSON(user)
|
2020-03-04 18:37:41 +01:00
|
|
|
},
|
2022-02-11 13:22:57 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Use: "password <old_password> <password> <user_auth_token>",
|
|
|
|
Short: "Update password",
|
2023-06-14 12:40:37 +02:00
|
|
|
Long: "Update password\n" +
|
|
|
|
"Usage:\n" +
|
|
|
|
"\tmainflux-cli users password old_password new_password $USERTOKEN\n",
|
2020-03-04 18:37:41 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 3 {
|
2022-02-11 13:22:57 +01:00
|
|
|
logUsage(cmd.Use)
|
2020-03-04 18:37:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
user, err := sdk.UpdatePassword(args[0], args[1], args[2])
|
|
|
|
if err != nil {
|
2020-03-04 18:37:41 +01:00
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logJSON(user)
|
2022-08-11 19:58:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Use: "enable <user_id> <user_auth_token>",
|
|
|
|
Short: "Change user status to enabled",
|
2023-06-14 12:40:37 +02:00
|
|
|
Long: "Change user status to enabled\n" +
|
|
|
|
"Usage:\n" +
|
|
|
|
"\tmainflux-cli users enable <user_id> <user_auth_token>\n",
|
2022-08-11 19:58:45 +03:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 2 {
|
|
|
|
logUsage(cmd.Use)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
user, err := sdk.EnableUser(args[0], args[1])
|
|
|
|
if err != nil {
|
2022-08-11 19:58:45 +03:00
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logJSON(user)
|
2022-08-11 19:58:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Use: "disable <user_id> <user_auth_token>",
|
|
|
|
Short: "Change user status to disabled",
|
2023-06-14 12:40:37 +02:00
|
|
|
Long: "Change user status to disabled\n" +
|
|
|
|
"Usage:\n" +
|
|
|
|
"\tmainflux-cli users disable <user_id> <user_auth_token>\n",
|
2022-08-11 19:58:45 +03:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) != 2 {
|
|
|
|
logUsage(cmd.Use)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
user, err := sdk.DisableUser(args[0], args[1])
|
|
|
|
if err != nil {
|
2022-08-11 19:58:45 +03:00
|
|
|
logError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:40:37 +02:00
|
|
|
logJSON(user)
|
2020-03-04 18:37:41 +01:00
|
|
|
},
|
2022-02-11 13:22:57 +01:00
|
|
|
},
|
|
|
|
}
|
2018-07-11 13:53:37 +02:00
|
|
|
|
2022-02-11 13:22:57 +01:00
|
|
|
// NewUsersCmd returns users command.
|
|
|
|
func NewUsersCmd() *cobra.Command {
|
2018-07-11 13:53:37 +02:00
|
|
|
cmd := cobra.Command{
|
2022-08-11 19:58:45 +03:00
|
|
|
Use: "users [create | get | update | token | password | enable | disable]",
|
2019-02-01 23:17:09 +01:00
|
|
|
Short: "Users management",
|
|
|
|
Long: `Users management: create accounts and tokens"`,
|
2020-10-31 23:29:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:44:21 +02:00
|
|
|
for i := range cmdUsers {
|
2018-07-11 13:53:37 +02:00
|
|
|
cmd.AddCommand(&cmdUsers[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cmd
|
|
|
|
}
|