• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "edm_command.h"
17 
18 #include <iostream>
19 #include <string_ex.h>
20 
21 #include "element_name.h"
22 
23 namespace OHOS {
24 namespace EDM {
25 constexpr int32_t ARR_INDEX_ZERO = 0;
26 constexpr int32_t ARR_INDEX_ONE = 1;
27 constexpr int32_t ARR_INDEX_TWO = 2;
28 
29 constexpr size_t ARGS_SIZE_TWO = 2;
30 constexpr size_t ARGS_SIZE_THREE = 3;
31 
EdmCommand(int argc,char * argv[])32 EdmCommand::EdmCommand(int argc, char *argv[])
33     : ShellCommand(argc, argv, TOOL_NAME)
34 {}
35 
CreateCommandMap()36 ErrCode EdmCommand::CreateCommandMap()
37 {
38     commandMap_ = {
39         { "help", std::bind(&EdmCommand::RunAsHelpCommand, this) },
40         { "enable-admin", std::bind(&EdmCommand::RunAsEnableAdminCommand, this) },
41         { "enable-super-admin",
42         std::bind(&EdmCommand::RunAsEnableSuperAdminCommand, this) },
43         { "disable-admin",
44         std::bind(&EdmCommand::RunDisableNormalAdminCommand, this) },
45     };
46 
47     return ERR_OK;
48 }
49 
CreateMessageMap()50 ErrCode EdmCommand::CreateMessageMap()
51 {
52     messageMap_ = { //  error + message
53         {
54             ERR_EDM_ADD_ADMIN_FAILED,
55             "error: add admin internal error.",
56         },
57         {
58             ERR_EDM_PERMISSION_ERROR,
59             "error: permission check failed.",
60         },
61         {
62             ERR_EDM_BMS_ERROR,
63             "error: get bundle manager or query infos failed.",
64         },
65         {
66             ERR_EDM_DENY_PERMISSION,
67             "error: permission requested by app is not allowed.",
68         },
69         {
70             ERR_EDM_DEL_ADMIN_FAILED,
71             "error: disable admin internal error.",
72         },
73         {
74             ERR_EDM_GET_PLUGIN_MGR_FAILED,
75             "error: get plugin manager internal error.",
76         },
77         {
78             ERR_EDM_PARAM_ERROR,
79             "error: param count or value invalid",
80         }
81     };
82 
83     return ERR_OK;
84 }
85 
init()86 ErrCode EdmCommand::init()
87 {
88     if (!enterpriseDeviceMgrProxy_) {
89         enterpriseDeviceMgrProxy_ = EnterpriseDeviceMgrProxy::GetInstance();
90     }
91     return ERR_OK;
92 }
93 
RunAsHelpCommand()94 ErrCode EdmCommand::RunAsHelpCommand()
95 {
96     resultReceiver_.append(HELP_MSG);
97 
98     return ERR_OK;
99 }
100 
RunAsEnableAdminCommand()101 ErrCode EdmCommand::RunAsEnableAdminCommand()
102 {
103     return RunAsEnableCommand(AdminType::NORMAL);
104 }
105 
RunAsEnableSuperAdminCommand()106 ErrCode EdmCommand::RunAsEnableSuperAdminCommand()
107 {
108     return RunAsEnableCommand(AdminType::ENT);
109 }
110 
RunAsEnableCommand(AdminType type)111 ErrCode EdmCommand::RunAsEnableCommand(AdminType type)
112 {
113     std::cout << "argc = " << argc_ << std::endl;
114     if (argc_ != ARGS_SIZE_THREE) {
115         resultReceiver_.append(GetMessageFromCode(ERR_EDM_PARAM_ERROR));
116         return ERR_EDM_PARAM_ERROR;
117     }
118     std::vector<std::string> elementNameStr = split(argv_[ARR_INDEX_TWO], "/");
119     if (elementNameStr.size() != ARGS_SIZE_TWO) {
120         resultReceiver_.append(GetMessageFromCode(ERR_EDM_PARAM_ERROR));
121         return ERR_EDM_PARAM_ERROR;
122     }
123     OHOS::AppExecFwk::ElementName elementName;
124     elementName.SetElementBundleName(&elementName, elementNameStr[ARR_INDEX_ZERO].c_str());
125     elementName.SetElementAbilityName(&elementName, elementNameStr[ARR_INDEX_ONE].c_str());
126     EntInfo info;
127     ErrCode result = enterpriseDeviceMgrProxy_->EnableAdmin(elementName, info, type, DEFAULT_USER_ID);
128     if (result != ERR_OK) {
129         resultReceiver_.append(GetMessageFromCode(result));
130     }
131     return result;
132 }
133 
split(const std::string & str,const std::string & pattern)134 std::vector<std::string> EdmCommand::split(const std::string &str, const std::string &pattern)
135 {
136     std::vector<std::string> res;
137     if (str == "") {
138         return res;
139     }
140     std::string strs = str + pattern;
141     size_t pos = strs.find(pattern);
142 
143     while (pos != strs.npos) {
144         std::string temp = strs.substr(0, pos);
145         res.push_back(temp);
146         strs = strs.substr(pos + 1, strs.size());
147         pos = strs.find(pattern);
148     }
149     return res;
150 }
151 
RunDisableNormalAdminCommand()152 ErrCode EdmCommand::RunDisableNormalAdminCommand()
153 {
154     std::cout << "argc = " << argc_ << std::endl;
155     if (argc_ != ARGS_SIZE_THREE) {
156         resultReceiver_.append(GetMessageFromCode(ERR_EDM_PARAM_ERROR));
157         return ERR_EDM_PARAM_ERROR;
158     }
159     for (int i = 0; i < argc_; ++i) {
160         std::cout << "argv[" << i << "] = " << argv_[i] << std::endl;
161     }
162     std::vector<std::string> elementNameStr = split(argv_[ARR_INDEX_TWO], "/");
163     std::cout << "elementNameStr.size() = " << elementNameStr.size() << std::endl;
164     if (elementNameStr.size() != ARGS_SIZE_TWO) {
165         resultReceiver_.append(GetMessageFromCode(ERR_EDM_PARAM_ERROR));
166         return ERR_EDM_PARAM_ERROR;
167     }
168     OHOS::AppExecFwk::ElementName elementName;
169     elementName.SetElementBundleName(&elementName, elementNameStr[ARR_INDEX_ZERO].c_str());
170     elementName.SetElementAbilityName(&elementName, elementNameStr[ARR_INDEX_ONE].c_str());
171     ErrCode result = enterpriseDeviceMgrProxy_->DisableAdmin(elementName, DEFAULT_USER_ID);
172     if (result != ERR_OK) {
173         resultReceiver_.append(GetMessageFromCode(result));
174     }
175     return result;
176 }
177 } // namespace EDM
178 } // namespace OHOS
179