1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BASE_ACCOUNT_OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H 17 #define BASE_ACCOUNT_OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H 18 19 #include "os_account.h" 20 #include "shell_command.h" 21 22 namespace OHOS { 23 namespace AccountSA { 24 namespace { 25 const std::string TOOL_NAME = "acm"; 26 27 const std::string HELP_MSG = "usage: acm <command> [<options>]\n" 28 "These are acm commands list:\n" 29 " help list available commands\n" 30 " create create a local account with options\n" 31 " delete delete a local account with options\n" 32 " switch switch to a local account with options\n" 33 " set set constraints of a local account\n" 34 " dump dump the info of local accounts\n"; 35 36 const std::string HELP_MSG_CREATE = 37 "usage: acm create <options>\n" 38 "options list:\n" 39 " -h, --help list available commands\n" 40 " -n <local-account-name> -t <type> create a local account with a name and a type\n" 41 " <type>: admin, normal, guest\n"; 42 43 const std::string HELP_MSG_DELETE = 44 "usage: acm delete <options>\n" 45 "options list:\n" 46 " -h, --help list available commands\n" 47 " -i <local-account-id> delete a local account with an id\n"; 48 49 const std::string HELP_MSG_DUMP = 50 "usage: acm dump <options>\n" 51 "options list:\n" 52 " -h, --help list available commands\n" 53 " -a, --all dump all local accounts\n" 54 " -i <local-account-id> dump a local account with an id\n"; 55 56 const std::string HELP_MSG_SET = 57 "usage: acm set <options>\n" 58 "options list:\n" 59 " -h, --help list available commands\n" 60 " -i <local-account-id> -c <constraints> [-e] set constraints for a local account\n"; 61 62 const std::string HELP_MSG_SWITCH = 63 "usage: acm switch <options>\n" 64 "options list:\n" 65 " -h, --help list available commands\n" 66 " -i <local-account-id> switch a local account with an id\n"; 67 68 const std::string HELP_MSG_OPTION_REQUIRES_AN_ARGUMENT = "error: option requires an argument."; 69 const std::string HELP_MSG_NO_NAME_OPTION = "error: -n <local-account-name> is expected"; 70 const std::string HELP_MSG_NO_TYPE_OPTION = "error: -t <type> is expected"; 71 const std::string HELP_MSG_NO_ID_OPTION = "error: -i <local-account-id> is expected"; 72 const std::string HELP_MSG_NO_CONSTRAINTS_OPTION = "error: -c <constraints> is expected"; 73 const std::string HELP_MSG_INVALID_TYPE_ARGUMENT = "error: invalid type argument"; 74 const std::string HELP_MSG_INVALID_ID_ARGUMENT = "error: invalid id argument"; 75 76 const std::string STRING_CREATE_OS_ACCOUNT_OK = "create the local account successfully."; 77 const std::string STRING_CREATE_OS_ACCOUNT_NG = "error: failed to create the local account."; 78 const std::string STRING_DELETE_OS_ACCOUNT_OK = "delete the local account successfully."; 79 const std::string STRING_DELETE_OS_ACCOUNT_NG = "error: failed to delete the local account."; 80 const std::string STRING_DUMP_OS_ACCOUNT_NG = "error: failed to dump state."; 81 const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_OK = "set constraints for the local account successfully."; 82 const std::string STRING_SET_OS_ACCOUNT_CONSTRAINTS_NG = "error: failed to set constraints for the local account."; 83 const std::string STRING_SWITCH_OS_ACCOUNT_OK = "switch the local account successfully."; 84 const std::string STRING_SWITCH_OS_ACCOUNT_NG = "error: failed to switch the local account."; 85 } // namespace 86 87 class AccountCommand : public OHOS::AAFwk::ShellCommand { 88 public: 89 AccountCommand(int argc, char *argv[]); 90 ~AccountCommand() = default; 91 92 private: 93 ErrCode CreateCommandMap() override; 94 ErrCode CreateMessageMap() override; 95 ErrCode init() override; 96 97 ErrCode RunAsHelpCommand(void); 98 ErrCode RunAsCreateCommand(void); 99 ErrCode RunAsDeleteCommand(void); 100 ErrCode RunAsSwitchCommand(void); 101 ErrCode RunAsDumpCommand(void); 102 ErrCode RunAsSetCommand(void); 103 104 ErrCode RunAsCreateCommandError(void); 105 ErrCode RunAsCreateCommandMissingOptionArgument(void); 106 ErrCode RunAsCreateCommandExistentOptionArgument(const int &option, std::string &name, OsAccountType &type); 107 ErrCode RunAsDeleteCommandError(void); 108 ErrCode RunAsDeleteCommandMissingOptionArgument(void); 109 ErrCode RunAsDeleteCommandExistentOptionArgument(const int &option, int &id); 110 ErrCode RunAsDumpCommandError(void); 111 ErrCode RunAsDumpCommandMissingOptionArgument(void); 112 ErrCode RunAsDumpCommandExistentOptionArgument(const int &option, int &id); 113 ErrCode RunAsSetCommandError(void); 114 ErrCode RunAsSetCommandMissingOptionArgument(void); 115 ErrCode RunAsSetCommandExistentOptionArgument( 116 const int &option, int &id, std::vector<std::string> &constraints, bool &enable); 117 ErrCode RunAsSwitchCommandError(void); 118 ErrCode RunAsSwitchCommandMissingOptionArgument(void); 119 ErrCode RunAsSwitchCommandExistentOptionArgument(const int &option, int &id); 120 121 ErrCode AnalyzeTypeArgument(OsAccountType &type); 122 ErrCode AnalyzeLocalIdArgument(int &id); 123 ErrCode AnalyzeConstraintArgument(std::vector<std::string> &constraints); 124 125 private: 126 std::shared_ptr<OsAccount> osAccountPtr_; 127 }; 128 } // namespace AccountSA 129 } // namespace OHOS 130 131 #endif // BASE_ACCOUNT_OS_ACCOUNT_TOOLS_ACM_INCLUDE_ACCOUNT_COMMAND_H 132