package com.perforce.jbrown.examples; import java.util.HashMap; import java.util.List; import java.util.Map; import com.perforce.jbrown.utils.LameServerFactory; import com.perforce.jbrown.utils.LogCallBack; import com.perforce.jbrown.utils.MapCmdUtils; import com.perforce.p4java.exception.P4JavaException; import com.perforce.p4java.server.IOptionsServer; /** * Sample Class to show updating a User Spec for field "AuthMethod". * * @author jbrown * */ public class GetUserSpec { public static void main(String[] args) { com.perforce.p4java.Log.setLogCallback(new LogCallBack()); try { IOptionsServer server = LameServerFactory.getServer(); // client not required for getting users. String userName = "nolinks"; /* * The usual way to update a user's spec is with getUser(), call * setters, then update. <p> * Unfortunately, p4java 2015.2 does not support the AuthMethod * field, so can't user getUser() to update the AuthMethod. */ // IUser user = server.getUser(userName); // update the user to ldap updateUserAuthMethod(server, userName, "ldap"); } catch (Throwable e) { System.out.println("Exeception: " + e.getMessage()); e.printStackTrace(System.out); } } /** * Updates the user's AuthMethod. Error during the update * throws an exception. If this returns, the authMethod is now * the new value. * * @param server * usable server instance * @param userName * user to update * @param authMethod * new authMethod for user - either "perforce" or "ldap" * * @throws P4JavaException */ private static void updateUserAuthMethod(IOptionsServer server, String userName, String authMethod) throws P4JavaException { if (!("perforce".equals(authMethod) || "ldap".equals(authMethod))) { throw new IllegalArgumentException("Invalid authMethod value of '" + authMethod + "' provided."); } /** * retrieve all user fields - need to use MapCmdList() to see p4java's * unsupported fields. */ Map<String, Object> userMap = getUserAsMap(server, userName); if (!userMap.containsKey("User")) { // should never be here. throw new P4JavaException("Unable to retrieve spec for user: " + userName); } if (userMap.containsKey("AuthMethod")) { String val = (String) userMap.get("AuthMethod"); if (authMethod.equalsIgnoreCase(val)) { // System.out.println("already " + authMethod); return; } } /** * userMap contains a lot of fields that need not be sent back to * update: P4Java filters out those extra fields (it ignores them). * * Unfortunately it also filters out new server fields that P4Java * hasn't written code for, such as "AuthMethod" (P4Java 2015.2 does not * know about AuthMethod). * * So we use a hack to append the new fields to an existing field. I've * arbitrarily decided to use the email field as email is required in the * user spec. * * Replace the "Email" entry's value with it's current value plus the * authmethod field. */ userMap.put("Email", userMap.get("Email") + MapCmdUtils.buildField("AuthMethod", authMethod, false)); List<Map<String, Object>> serverMaps = server.execMapCmdList("user", new String[] { "-i", "-f" }, userMap); for (Map<String, Object> map : serverMaps) { MapCmdUtils.handleErrorStr(server, map); // exception thrown here if the update fails. } } /*** * Returns all fields for a user. See "p4 -Ztag user -o userName" for the * fields returned. * * @param server * @param userName * @return Map of all user fields. * @throws P4JavaException */ private static Map<String, Object> getUserAsMap(IOptionsServer server, String userName) throws P4JavaException { List<Map<String, Object>> serverMaps = server.execMapCmdList("user", new String[] { "-o", userName }, null); Map<String, Object> results = new HashMap<String, Object>(); for (Map<String, Object> map : serverMaps) { // user -o should only return one Map in the List. if (map != null) { if (!MapCmdUtils.handleErrorStr(server, map)) { for (String key : map.keySet()) { // Treat everything as a String. String val = (map.get(key)).toString(); results.put(key, val); } } } } return results; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 19914 | Joel Brown |
Example for updating a user's AuthMethod in p4java 2015.2. P4Java 2015.2 doesn't know squat about field AuthMethod. |