Last active
August 29, 2015 14:01
-
-
Save paully21/805fbaae2e24533647df to your computer and use it in GitHub Desktop.
Add a user to a Discourse group using RestSharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private bool setGroupAccess(string username, string groupName) | |
{ | |
string url = "/groups/{group_name}.json?api_key={api_key}&api_username={api_username}"; | |
var client = new RestClient(ConfigurationManager.AppSettings["DiscourseUrl"]); | |
var request = new RestRequest(url, Method.GET); | |
request.AddUrlSegment("group_name", groupName); | |
request.AddUrlSegment("api_key", ConfigurationManager.AppSettings["DiscourseAPIKey"]); | |
request.AddUrlSegment("api_username", ConfigurationManager.AppSettings["DiscourseAPIUsername"]); | |
request.RootElement = "basic_group"; | |
IRestResponse<DiscourseGroup> groupInfoResponse = client.Execute<DiscourseGroup>(request); | |
int groupId = groupInfoResponse.Data.id; | |
url = "/groups/{group_name}/members.json?api_key={api_key}&api_username={api_username}"; | |
client = new RestClient(ConfigurationManager.AppSettings["DiscourseUrl"]); | |
request = new RestRequest(url, Method.GET); | |
request.AddUrlSegment("group_name", groupName); | |
request.AddUrlSegment("api_key", ConfigurationManager.AppSettings["DiscourseAPIKey"]); | |
request.AddUrlSegment("api_username", ConfigurationManager.AppSettings["DiscourseAPIUsername"]); | |
IRestResponse<List<DiscourseGroupUserEntry>> response = client.Execute<List<DiscourseGroupUserEntry>>(request); | |
if (response.StatusCode == HttpStatusCode.OK && response.Data != null) | |
{ | |
List<string> usernames = response.Data.Select(c => c.username).ToList(); | |
if (!usernames.Contains(username)) | |
{ | |
usernames.Add(username); | |
string usernameString = string.Join(",", usernames); | |
url = "/admin/groups/{group_id}?api_key={api_key}&api_username={api_username}"; | |
request = new RestRequest(url, Method.PUT); | |
request.AddUrlSegment("group_id", groupId.ToString()); | |
request.AddUrlSegment("api_key", ConfigurationManager.AppSettings["DiscourseAPIKey"]); | |
request.AddUrlSegment("api_username", ConfigurationManager.AppSettings["DiscourseAPIUsername"]); | |
request.AddParameter("group[name]", groupName); | |
request.AddParameter("group[alias_level]", "0"); | |
request.AddParameter("group[usernames]", usernameString); | |
IRestResponse putResponse = client.Execute(request); | |
return response.StatusCode == HttpStatusCode.OK; | |
} | |
//already in the group | |
return true; | |
} | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DiscourseGroupUserEntry | |
{ | |
public string id { get; set; } | |
public string avatar_template { get; set; } | |
public string last_seen_at { get; set; } | |
public string name { get; set; } | |
public string username { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment