buffet: Added minimal role into command definition

Possible values "viewer", "user", "owner", "manager".
Default minimal role is set to "user".
Role check is not yet implemented.

BUG=brillo:808
TEST=`FEATURES=test emerge-gizmo buffet`

Change-Id: I74d16c24618b50de1e7a2ffa37d7aef36978a38a
Reviewed-on: https://chromium-review.googlesource.com/274117
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Tested-by: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/commands/command_dictionary.cc b/buffet/commands/command_dictionary.cc
index 0fdae65..4f66c7d 100644
--- a/buffet/commands/command_dictionary.cc
+++ b/buffet/commands/command_dictionary.cc
@@ -72,6 +72,7 @@
       const ObjectSchema* base_results_def = nullptr;
       // By default make it available to all clients.
       auto visibility = CommandDefinition::Visibility::GetAll();
+      UserRole minimal_role{UserRole::kUser};
       if (base_commands) {
         auto cmd = base_commands->FindCommand(full_command_name);
         if (cmd) {
@@ -79,6 +80,7 @@
           base_progress_def = cmd->GetProgress();
           base_results_def = cmd->GetResults();
           visibility = cmd->GetVisibility();
+          minimal_role = cmd->GetMinimalRole();
         }
 
         // If the base command dictionary was provided but the command was not
@@ -123,8 +125,8 @@
         return false;
 
       std::string value;
-      using commands::attributes::kCommand_Visibility;
-      if (command_def_json->GetString(kCommand_Visibility, &value)) {
+      if (command_def_json->GetString(commands::attributes::kCommand_Visibility,
+                                      &value)) {
         if (!visibility.FromString(value, error)) {
           chromeos::Error::AddToPrintf(
               error, FROM_HERE, errors::commands::kDomain,
@@ -134,12 +136,24 @@
         }
       }
 
+      if (command_def_json->GetString(commands::attributes::kCommand_Role,
+                                      &value)) {
+        if (!FromString(value, &minimal_role, error)) {
+          chromeos::Error::AddToPrintf(
+              error, FROM_HERE, errors::commands::kDomain,
+              errors::commands::kInvalidMinimalRole,
+              "Error parsing command '%s'", full_command_name.c_str());
+          return false;
+        }
+      }
+
       std::unique_ptr<CommandDefinition> command_def{
           new CommandDefinition{category,
                                 std::move(parameters_schema),
                                 std::move(progress_schema),
                                 std::move(results_schema)}};
       command_def->SetVisibility(visibility);
+      command_def->SetMinimalRole(minimal_role);
       new_defs.emplace(full_command_name, std::move(command_def));
 
       command_iter.Advance();
@@ -227,6 +241,8 @@
     base::DictionaryValue* command_def = new base::DictionaryValue;
     command_def->Set(commands::attributes::kCommand_Parameters,
                      parameters.release());
+    command_def->SetString(commands::attributes::kCommand_Role,
+                           ToString(pair.second->GetMinimalRole()));
     package->SetWithoutPathExpansion(command_name, command_def);
   }
   return dict;