Temporary repo to track my changes on LTS functions app porting
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.

207 lines
7.5 KiB

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Azure.Cosmos;
  3. using Microsoft.Extensions.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace CDP
  10. {
  11. public class CDPLite
  12. {
  13. private readonly ILogger<CDPLite> _logger;
  14. private static string FileAuditContainer = "FileAudits";
  15. private static string UserAuditContainer = "UserAudits";
  16. private static string GroupAuditContainer = "GroupAudits";
  17. private static string TenantAuditContainer = "TenantAudits";
  18. public CDPLite(ILogger<CDPLite> log)
  19. {
  20. _logger = log;
  21. }
  22. internal static async Task<IActionResult> AddFileUserInternal(AddFileUserDto dto)
  23. {
  24. // check to see if the email has the power to add a user
  25. string userId = Helpers.HashAndShortenText(dto.Email.ToLower());
  26. FileRecord fr = await CDPDB.GetFile(dto.AppKey, dto.FileId, userId);
  27. if (fr == null)
  28. {
  29. string message = string.Format($"{dto.Email} attempted to add/change access policy for {dto.EmailToAdd} on {dto.FileName} file having {dto.FileId} id, but didn't have ANY access");
  30. Console.WriteLine(message);
  31. string action = "Policy change failed";
  32. await AddAudits(dto.AppKey, dto.FileId, dto.FileName, userId, "", action, message);
  33. return new BadRequestObjectResult(new { error = true, message = "File not found for user " + dto.Email });
  34. }
  35. if ((!fr.Policy.CheckAccess("Manage")) && (!fr.Policy.CheckAccess("Owner")))
  36. {
  37. string message = string.Format($"{dto.Email} attempted to add/change access policy for {dto.EmailToAdd} on {dto.FileName} file having {dto.FileId} id, but didn't have manage access");
  38. Console.WriteLine(message);
  39. string action = "Policy change failed";
  40. await AddAudits(dto.AppKey, dto.FileId, dto.FileName, userId, "", action, message);
  41. return new BadRequestObjectResult(new { error = true, message = $"{dto.Email} doesn't have the rights to add a user." });
  42. }
  43. string fileId = dto.FileId;
  44. string fileName = dto.FileName;
  45. string userIdToAdd = "";
  46. if (dto.EmailToAdd != "")
  47. {
  48. userIdToAdd = Helpers.HashAndShortenText(dto.EmailToAdd.ToLower());
  49. }
  50. else if (dto.Group != null)
  51. {
  52. userIdToAdd = dto.GroupId;
  53. }
  54. else if (dto.Group != null)
  55. {
  56. userIdToAdd = dto.GroupId;
  57. }
  58. AccessPolicy ac = new AccessPolicy()
  59. {
  60. Access = dto.Policy,
  61. Email = dto.EmailToAdd.ToLower(),
  62. Group = dto.Group,
  63. GroupId = dto.GroupId,
  64. Key = ""
  65. };
  66. fr = await CDPDB.UpsertFile(dto.AppKey, fileId, fileName, userIdToAdd, "", ac);
  67. if (dto.EmailToAdd != "")
  68. {
  69. string message = string.Format($"{dto.Email} added/changed the access policy for User : {dto.EmailToAdd} to {dto.Policy} on {fileName} file having {fileId} id");
  70. string action = "Policy change";
  71. await AddAudits(dto.AppKey, fileId, fileName, userId, "", action, message);
  72. }
  73. if (dto.Group != null)
  74. {
  75. string message = string.Format($"{dto.Email} added/changed the access policy for Group : {dto.Group} to {dto.Policy} on {fileName} file having {fileId} id");
  76. string action = "Policy change";
  77. await AddAudits(dto.AppKey, fileId, fileName, "", dto.Group.id, action, message);
  78. }
  79. return new OkObjectResult(fr);
  80. }
  81. public static async Task AddAudits(string appKey, string fileId, string fileName, string userId, string groupid, string action, string message)
  82. {
  83. if (string.IsNullOrEmpty(appKey) || string.IsNullOrEmpty(fileId) || string.IsNullOrEmpty(action) || string.IsNullOrEmpty(message))
  84. {
  85. Console.WriteLine(string.Format("something weird? appKey, fileId, action, message: {0} {1} {2} {3}", appKey, fileId, action, message));
  86. return;
  87. }
  88. AuditRecord faRec = new FileAuditRecord()
  89. {
  90. AppKey = appKey,
  91. FileId = fileId,
  92. FileName = fileName,
  93. UserId = userId,
  94. GroupId = groupid,
  95. Action = action,
  96. Message = message,
  97. EventTime = DateTime.UtcNow,
  98. };
  99. Console.WriteLine("Adding File Audit Record");
  100. await AuditDB.AppendRecord(faRec.id, faRec, FileAuditContainer);
  101. AuditRecord faRecTenant = new TenantAuditRecord()
  102. {
  103. AppKey = appKey,
  104. FileId = fileId,
  105. FileName = fileName,
  106. UserId = userId,
  107. GroupId = groupid,
  108. Action = action,
  109. Message = message,
  110. EventTime = DateTime.UtcNow,
  111. };
  112. await AuditDB.AppendRecord(faRecTenant.id, faRecTenant, TenantAuditContainer);
  113. if (!string.IsNullOrEmpty(groupid))
  114. {
  115. AuditRecord faRecGroup = new GroupAuditRecord()
  116. {
  117. AppKey = appKey,
  118. FileId = fileId,
  119. FileName = fileName,
  120. UserId = userId,
  121. GroupId = groupid,
  122. Action = action,
  123. Message = message,
  124. EventTime = DateTime.UtcNow,
  125. };
  126. await AuditDB.AppendRecord(faRecGroup.id, faRecGroup, GroupAuditContainer);
  127. }
  128. AuditRecord faRecUser = new UserAuditRecord()
  129. {
  130. AppKey = appKey,
  131. FileId = fileId,
  132. FileName = fileName,
  133. UserId = userId,
  134. GroupId = groupid,
  135. Action = action,
  136. Message = message,
  137. EventTime = DateTime.UtcNow,
  138. };
  139. await AuditDB.AppendRecord(faRecUser.id, faRecUser, UserAuditContainer);
  140. }
  141. /// <summary>
  142. /// Adds the audit record on a background thread.
  143. /// </summary>
  144. private static async Task AddFileAudit(AuditRecord far)
  145. {
  146. await AuditDB.AppendRecord(far.id, far, FileAuditContainer);
  147. }
  148. private static async Task AddUserAudit(AuditRecord far)
  149. {
  150. await AuditDB.AppendRecord(far.id, far, UserAuditContainer);
  151. }
  152. private static async Task AddTenantAudit(AuditRecord far)
  153. {
  154. await Task.Run(async () =>
  155. {
  156. try
  157. {
  158. await AuditDB.AppendRecord(far.id, far, TenantAuditContainer);
  159. }
  160. catch (Exception e)
  161. {
  162. }
  163. });
  164. }
  165. private static async Task AddGroupAudit(AuditRecord far)
  166. {
  167. await Task.Run(async () =>
  168. {
  169. try
  170. {
  171. await AuditDB.AppendRecord(far.id, far, GroupAuditContainer);
  172. }
  173. catch (Exception e)
  174. {
  175. }
  176. });
  177. }
  178. }
  179. }