The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GiteaService.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. namespace Teknik.GitService
  7. {
  8. public class GiteaService : IGitService
  9. {
  10. private readonly int _sourceId;
  11. private readonly string _host;
  12. private readonly string _accessToken;
  13. private readonly string _server;
  14. private readonly string _database;
  15. private readonly string _username;
  16. private readonly string _password;
  17. private readonly int _port;
  18. public GiteaService(int sourceId, string host, string accessToken, string server, string database, string username, string password, int port)
  19. {
  20. _sourceId = sourceId;
  21. _host = host;
  22. _accessToken = accessToken;
  23. _server = server;
  24. _database = database;
  25. _username = username;
  26. _password = password;
  27. _port = port;
  28. }
  29. public bool AccountExists(string username)
  30. {
  31. Uri baseUri = new Uri(_host);
  32. Uri finalUri = new Uri(baseUri, "api/v1/users/" + username + "?token=" + _accessToken);
  33. WebRequest request = WebRequest.Create(finalUri);
  34. request.Method = "GET";
  35. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  36. if (response.StatusCode == HttpStatusCode.OK)
  37. {
  38. return true;
  39. }
  40. return false;
  41. }
  42. public void CreateAccount(string username, string email, string password)
  43. {
  44. // Add gogs user
  45. using (var client = new WebClient())
  46. {
  47. var obj = new { source_id = _sourceId, username = username, email = email, login_name = email, password = password };
  48. string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  49. client.Headers[HttpRequestHeader.ContentType] = "application/json";
  50. Uri baseUri = new Uri(_host);
  51. Uri finalUri = new Uri(baseUri, "api/v1/admin/users?token=" + _accessToken);
  52. string result = client.UploadString(finalUri, "POST", json);
  53. }
  54. }
  55. public void DeleteAccount(string username)
  56. {
  57. try
  58. {
  59. Uri baseUri = new Uri(_host);
  60. Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + username + "?token=" + _accessToken);
  61. WebRequest request = WebRequest.Create(finalUri);
  62. request.Method = "DELETE";
  63. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  64. if (response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.NoContent)
  65. {
  66. throw new Exception("Response Code: " + response.StatusCode);
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. // This error signifies the user doesn't exist, so we can continue deleting
  72. if (ex.Message != "The remote server returned an error: (404) Not Found.")
  73. {
  74. throw new Exception("Unable to delete git account. Exception: " + ex.Message);
  75. }
  76. }
  77. }
  78. public void EditPassword(string username, string email, string password)
  79. {
  80. using (var client = new WebClient())
  81. {
  82. var obj = new { source_id = _sourceId, email = email, login_name = email, password = password };
  83. string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  84. client.Headers[HttpRequestHeader.ContentType] = "application/json";
  85. Uri baseUri = new Uri(_host);
  86. Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + username + "?token=" + _accessToken);
  87. string result = client.UploadString(finalUri, "PATCH", json);
  88. }
  89. }
  90. public void EnableAccount(string username, string email)
  91. {
  92. ChangeAccountStatus(username, email, true);
  93. }
  94. public void DisableAccount(string username, string email)
  95. {
  96. ChangeAccountStatus(username, email, false);
  97. }
  98. public void ChangeAccountStatus(string username, string email, bool active)
  99. {
  100. using (var client = new WebClient())
  101. {
  102. var obj = new { active = active, email = email };
  103. string json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  104. client.Headers[HttpRequestHeader.ContentType] = "application/json";
  105. Uri baseUri = new Uri(_host);
  106. Uri finalUri = new Uri(baseUri, "api/v1/admin/users/" + username + "?token=" + _accessToken);
  107. string result = client.UploadString(finalUri, "PATCH", json);
  108. }
  109. }
  110. public DateTime LastActive(string email)
  111. {
  112. // We need to check the actual git database
  113. MysqlDatabase mySQL = new MysqlDatabase(_server, _database, _username, _password, _port);
  114. string sql = @"SELECT
  115. CASE
  116. WHEN MAX(gogs.action.created) >= MAX(gogs.user.updated) THEN MAX(gogs.action.created)
  117. WHEN MAX(gogs.user.updated) >= MAX(gogs.action.created) THEN MAX(gogs.user.updated)
  118. ELSE MAX(gogs.user.updated)
  119. END AS LastUpdate
  120. FROM gogs.user
  121. LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
  122. WHERE gogs.user.login_name = {0}";
  123. var results = mySQL.Query(sql, new object[] { email });
  124. DateTime lastActive = new DateTime(1900, 1, 1);
  125. if (results != null && results.Any())
  126. {
  127. var result = results.First();
  128. DateTime.TryParse(result["LastUpdate"].ToString(), out lastActive);
  129. }
  130. return lastActive;
  131. }
  132. }
  133. }