user_channel: Rewriting ipmiUserSetUserName API

Rewriting ipmiUserSetUserName API

Tested:
Verified using ipmitool commands.
Command: ipmitool user set name 4 user4
Response:    //Success
Command: ipmitool user set password 4 asdf1234
Response: Set User Password command successful (user 4)
Command: ipmitool user priv 4 0x03 1
Response: Set Privilege Level command successful (user 4)
Command: ipmitool user set name 14 user13asdfghkjlqwert
Response: Username is too long (> 16 bytes)
Command: ipmitool -I lanplus -C 3 -p 623 -U root -P <password> -H
         <BMC-IP> user set name 8 WIJGueNKd
Response:                       //Success
Command: ipmitool user list 1 //User list for channel 1
1   root             false   true       true       ADMINISTRATOR
2   user2            true    false      false      USER
3   user3            true    false      false      NO ACCESS
4   user4            true    false      false      OPERATOR
5   WIJGueNK         true    false      false      NO ACCESS
6   WIJGueNKb        true    false      false      NO ACCESS
7   WIJGueNKc        true    false      false      NO ACCESS
8   WIJGueNKd        true    false      false      NO ACCESS
9                    true    false      false      NO ACCESS
10                   true    false      false      NO ACCESS

Signed-off-by: jayaprakash Mutyala <mutyalax.jayaprakash@intel.com>
Change-Id: I41c091f6d9aaf54326295d1e80e16db521b2e23d
diff --git a/user_channel/user_layer.cpp b/user_channel/user_layer.cpp
index 5e4aed7..df04dc5 100644
--- a/user_channel/user_layer.cpp
+++ b/user_channel/user_layer.cpp
@@ -74,7 +74,14 @@
 
 Cc ipmiUserSetUserName(const uint8_t userId, const char* userName)
 {
-    return getUserAccessObject().setUserName(userId, userName);
+    std::string newUser(userName, 0, ipmiMaxUserName);
+    return getUserAccessObject().setUserName(userId, newUser);
+}
+
+Cc ipmiUserSetUserName(const uint8_t userId, const std::string& userName)
+{
+    std::string newUser(userName, 0, ipmiMaxUserName);
+    return getUserAccessObject().setUserName(userId, newUser);
 }
 
 Cc ipmiUserGetUserName(const uint8_t userId, std::string& userName)
diff --git a/user_channel/user_layer.hpp b/user_channel/user_layer.hpp
index b0d0d4c..0639474 100644
--- a/user_channel/user_layer.hpp
+++ b/user_channel/user_layer.hpp
@@ -143,6 +143,15 @@
  */
 Cc ipmiUserSetUserName(const uint8_t userId, const char* userName);
 
+/** @brief set's user name
+ *
+ *  @param[in] userId - user id
+ *  @param[in] userName - user name
+ *
+ *  @return ccSuccess for success, others for failure.
+ */
+Cc ipmiUserSetUserName(const uint8_t userId, const std::string& userName);
+
 /** @brief set user password
  *
  *  @param[in] userId - user id
diff --git a/user_channel/user_mgmt.cpp b/user_channel/user_mgmt.cpp
index 14978a1..e1e21c9 100644
--- a/user_channel/user_mgmt.cpp
+++ b/user_channel/user_mgmt.cpp
@@ -554,14 +554,13 @@
     }
 }
 
-bool UserAccess::isValidUserName(const char* userNameInChar)
+bool UserAccess::isValidUserName(const std::string& userName)
 {
-    if (!userNameInChar)
+    if (userName.empty())
     {
-        log<level::ERR>("null ptr");
+        log<level::ERR>("userName is empty");
         return false;
     }
-    std::string userName(userNameInChar, 0, ipmiMaxUserName);
     if (!std::regex_match(userName.c_str(),
                           std::regex("[a-zA-z_][a-zA-Z_0-9]*")))
     {
@@ -964,7 +963,7 @@
     return false;
 }
 
-Cc UserAccess::setUserName(const uint8_t userId, const char* userNameInChar)
+Cc UserAccess::setUserName(const uint8_t userId, const std::string& userName)
 {
     if (!isValidUserId(userId))
     {
@@ -976,15 +975,15 @@
     std::string oldUser;
     getUserName(userId, oldUser);
 
-    std::string newUser(userNameInChar, 0, ipmiMaxUserName);
-    if (oldUser == newUser)
+    if (oldUser == userName)
     {
         // requesting to set the same user name, return success.
         return ccSuccess;
     }
-    bool validUser = isValidUserName(userNameInChar);
+
+    bool validUser = isValidUserName(userName);
     UserInfo* userInfo = getUserInfo(userId);
-    if (newUser.empty() && !oldUser.empty())
+    if (userName.empty() && !oldUser.empty())
     {
         // Delete existing user
         std::string userPath = std::string(userObjBasePath) + "/" + oldUser;
@@ -1004,7 +1003,7 @@
         }
         deleteUserIndex(userId);
     }
-    else if (oldUser.empty() && !newUser.empty() && validUser)
+    else if (oldUser.empty() && !userName.empty() && validUser)
     {
         try
         {
@@ -1016,7 +1015,7 @@
             auto method = bus.new_method_call(
                 getUserServiceName().c_str(), userMgrObjBasePath,
                 userMgrInterface, createUserMethod);
-            method.append(newUser.c_str(), availableGroups, "", false);
+            method.append(userName.c_str(), availableGroups, "", false);
             auto reply = bus.call(method);
         }
         catch (const sdbusplus::exception::SdBusError& e)
@@ -1026,10 +1025,13 @@
                               entry("PATH=%s", userMgrObjBasePath));
             return ccUnspecifiedError;
         }
-        std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName);
+
+        std::memset(userInfo->userName, 0, sizeof(userInfo->userName));
+        std::memcpy(userInfo->userName,
+                    static_cast<const void*>(userName.data()), userName.size());
         userInfo->userInSystem = true;
     }
-    else if (oldUser != newUser && validUser)
+    else if (oldUser != userName && validUser)
     {
         try
         {
@@ -1037,7 +1039,7 @@
             auto method = bus.new_method_call(
                 getUserServiceName().c_str(), userMgrObjBasePath,
                 userMgrInterface, renameUserMethod);
-            method.append(oldUser.c_str(), newUser.c_str());
+            method.append(oldUser.c_str(), userName.c_str());
             auto reply = bus.call(method);
         }
         catch (const sdbusplus::exception::SdBusError& e)
@@ -1051,8 +1053,12 @@
                   static_cast<uint8_t*>(userInfo->userName) +
                       sizeof(userInfo->userName),
                   0);
-        std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName);
-        ipmiRenameUserEntryPassword(oldUser, newUser);
+
+        std::memset(userInfo->userName, 0, sizeof(userInfo->userName));
+        std::memcpy(userInfo->userName,
+                    static_cast<const void*>(userName.data()), userName.size());
+
+        ipmiRenameUserEntryPassword(oldUser, userName);
         userInfo->userInSystem = true;
     }
     else if (!validUser)
diff --git a/user_channel/user_mgmt.hpp b/user_channel/user_mgmt.hpp
index d77317a..20abda1 100644
--- a/user_channel/user_mgmt.hpp
+++ b/user_channel/user_mgmt.hpp
@@ -164,7 +164,7 @@
      *
      *  @return true if valid, false otherwise
      */
-    bool isValidUserName(const char* userNameInChar);
+    bool isValidUserName(const std::string& userName);
 
     /** @brief determines whether ipmi is in available groups list
      *
@@ -213,7 +213,7 @@
      *
      *  @return ccSuccess for success, others for failure.
      */
-    Cc setUserName(const uint8_t userId, const char* userNameInChar);
+    Cc setUserName(const uint8_t userId, const std::string& userName);
 
     /** @brief to set user enabled state
      *