(cmd *Command, args []string)
| 70 | } |
| 71 | |
| 72 | func runGrant(cmd *Command, args []string) { |
| 73 | commonFlagsInit(cmd) |
| 74 | |
| 75 | if len(args) > 0 || toUser == "" || toDSN == "" || perm == "" { |
| 76 | ConsoleLog.Error("grant command need to-user, to-dsn address and permission struct as param") |
| 77 | SetExitStatus(1) |
| 78 | printCommandHelp(cmd) |
| 79 | Exit() |
| 80 | } |
| 81 | |
| 82 | if !strings.HasPrefix(toDSN, client.DBScheme) && !strings.HasPrefix(toDSN, client.DBSchemeAlias) { |
| 83 | ConsoleLog.Error("grant permission failed: invalid dsn provided, use address start with 'covenantsql://'") |
| 84 | SetExitStatus(1) |
| 85 | return |
| 86 | } |
| 87 | toDSN = strings.TrimLeft(toDSN, client.DBScheme+"://") |
| 88 | toDSN = strings.TrimLeft(toDSN, client.DBSchemeAlias+"://") |
| 89 | |
| 90 | targetUserHash, err := hash.NewHashFromStr(toUser) |
| 91 | if err != nil { |
| 92 | ConsoleLog.WithError(err).Error("target user address is not valid") |
| 93 | SetExitStatus(1) |
| 94 | return |
| 95 | } |
| 96 | targetUser := proto.AccountAddress(*targetUserHash) |
| 97 | |
| 98 | targetChainHash, err := hash.NewHashFromStr(toDSN) |
| 99 | if err != nil { |
| 100 | ConsoleLog.WithError(err).Error("target dsn address is not valid") |
| 101 | SetExitStatus(1) |
| 102 | return |
| 103 | } |
| 104 | targetChain := proto.AccountAddress(*targetChainHash) |
| 105 | |
| 106 | var permPayload userPermPayload |
| 107 | |
| 108 | if err := json.Unmarshal([]byte(perm), &permPayload); err != nil { |
| 109 | // try again using role string representation |
| 110 | permPayload.Role.FromString(perm) |
| 111 | // invalid struct will set to void |
| 112 | if permPayload.Role == types.Void && !strings.EqualFold(perm, "Void") { |
| 113 | ConsoleLog.WithError(err).Errorf("update permission failed: invalid permission description") |
| 114 | SetExitStatus(1) |
| 115 | return |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | p := &types.UserPermission{ |
| 120 | Role: permPayload.Role, |
| 121 | Patterns: permPayload.Patterns, |
| 122 | } |
| 123 | |
| 124 | if !p.IsValid() { |
| 125 | ConsoleLog.Errorf("update permission failed: invalid permission description") |
| 126 | SetExitStatus(1) |
| 127 | return |
| 128 | } |
| 129 |
nothing calls this directly
no test coverage detected