| 152 | } |
| 153 | |
| 154 | static CoTryTask<Dispatcher::OutputTable> handle(IEnv &ienv, |
| 155 | const argparse::ArgumentParser &parser, |
| 156 | const Dispatcher::Args &args) { |
| 157 | auto &env = dynamic_cast<AdminEnv &>(ienv); |
| 158 | ENSURE_USAGE(args.empty()); |
| 159 | Dispatcher::OutputTable output; |
| 160 | |
| 161 | auto uid = Uid(parser.get<uint32_t>("uid")); |
| 162 | auto name = parser.present("--name"); |
| 163 | auto isRootValue = parser.present<int32_t>("--root"); |
| 164 | auto isAdminValue = parser.present<int32_t>("--admin"); |
| 165 | auto groups = splitAndTransform(parser.present<String>("--groups").value_or(""), boost::is_any_of(","), [](auto s) { |
| 166 | return flat::Gid(folly::to<uint32_t>(s)); |
| 167 | }); |
| 168 | |
| 169 | if (isRootValue) { |
| 170 | ENSURE_USAGE(*isRootValue == 0 || *isRootValue == 1, "-r should be 0 or 1"); |
| 171 | } |
| 172 | if (isAdminValue) { |
| 173 | ENSURE_USAGE(*isAdminValue == 0 || *isAdminValue == 1, "--admin should be 0 or 1"); |
| 174 | } |
| 175 | |
| 176 | auto toBool = [](auto v) -> bool { return v; }; |
| 177 | |
| 178 | std::optional<bool> isRoot = optionalMap(isRootValue, toBool); |
| 179 | std::optional<bool> isAdmin = optionalMap(isAdminValue, toBool); |
| 180 | |
| 181 | core::UserStore store; |
| 182 | auto handler = [&](kv::IReadWriteTransaction &txn) -> CoTryTask<meta::UserAttr> { |
| 183 | CO_RETURN_ON_ERROR(co_await ensureAdmin(txn, store, env.userInfo.token)); |
| 184 | co_return co_await store.setUserAttr(txn, |
| 185 | uid, |
| 186 | name ? std::string_view(*name) : std::string_view{}, |
| 187 | groups.empty() ? nullptr : &groups, |
| 188 | isRoot, |
| 189 | isAdmin); |
| 190 | }; |
| 191 | auto commitRes = co_await kv::WithTransaction(getRetryStrategy()) |
| 192 | .run(env.kvEngineGetter()->createReadWriteTransaction(), std::move(handler)); |
| 193 | CO_RETURN_ON_ERROR(commitRes); |
| 194 | printUserAttr(output, uid, *commitRes); |
| 195 | |
| 196 | co_return output; |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | struct UserSetTokenHandler { |
nothing calls this directly
no test coverage detected