public HttpResponseMessage AddAdmin([FromUri] string accessToken, string computer, [FromBody] AddAdministratorRequest adminRequest) { bool isAuth = CheckpointAuth.CheckCheckpointToken(accessToken); HttpResponseMessage response = new HttpResponseMessage(); if (isAuth) { AddAdministratorResult result = ServerHandler.AddAdministratorsByRequestObject(computer, adminRequest); response = Request.CreateResponse(HttpStatusCode.OK, result); } else { AddAdministratorResult result = new AddAdministratorResult(computer); result.message = "Invalid Token"; response = Request.CreateResponse(HttpStatusCode.Forbidden, result); } return(response); }
/// <summary> /// This method receieves a server name and AdministratorRequest object and adds the /// users and groups from the request as admins to the specified server. /// </summary> /// <param name="serverName">The name of the server to add administators to.</param> /// <param name="request">A AddAdministratorRequest object that contains the users and groups that need to be /// added as administrators.</param> /// <returns>A AddAdministratorResult object containing the lists of users and groups that were added/were already admins/ /// weren't added and a result message.</returns> public static AddAdministratorResult AddAdministratorsByRequestObject(string serverName, AddAdministratorRequest request) { // Creating result object to return. AddAdministratorResult result = new AddAdministratorResult(serverName); // Checking that we receieved users that need to be added. if (request.users != null) { // Iterating all users and adding as administrators. foreach (string user in request.users) { switch (AddAdministrator(serverName, user)) { case 0: // Group added successfully. result.addedUsers.Add(user); break; case 1: // Group already administrator. result.alreadyAdminUsers.Add(user); break; case 2: // User doesn't exist in Active Directory. result.failedUsers.Add(user); result.message = "Some of the specified users or groups don't exist in Active Directory."; break; case 3: // Some administrators on the server were already deleted from AD, Wind can't query the administrators. result.failedUsers.Add(user); result.message = "Some administrators on the server were already deleted from AD, Wind can't query the administrators."; break; case 4: // The target server took to long to respond and connection the server failed. result.failedUsers.Add(user); result.message = "The target server took to long to respond and connection the server failed."; break; case 5: // An unidentified error occured. result.failedUsers.Add(user); result.message = "A general error occured."; break; default: // An unidentified error occured. result.message = "A general error occured."; break; } } } // Checking that we receieved groups that need to be added. if (request.groups != null) { // Iterating all groups and adding as administrators. foreach (string group in request.groups) { switch (AddAdministrator(serverName, group)) { case 0: // Group added successfully. result.addedGroups.Add(group); break; case 1: // Group already administrator. result.alreadyAdminGroups.Add(group); break; case 2: // Group doesn't exist in Active Directory. result.failedGroups.Add(group); result.message = "Some of the specified users or groups don't exist in Active Directory."; break; case 3: // Some administrators on the server were already deleted from AD, Wind can't query the administrators. result.failedGroups.Add(group); result.message = "Some administrators on the server were already deleted from AD, Wind can't query the administrators."; break; case 4: // The target server took to long to respond and connection the server failed. result.failedGroups.Add(group); result.message = "The target server took to long to respond and connection the server failed."; break; case 5: // An unidentified error occured. result.failedGroups.Add(group); result.message = "A general error occured."; break; default: // An unidentified error occured. result.message = "A general error occured."; break; } } } if (result.message == string.Empty) { // No error has occured, so return a message that everything worked fine. result.message = "All users and groups were added successfully."; } return(result); }