AccessControlList.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Text;
  11. namespace Aliyun.OSS
  12. {
  13. /// <summary>
  14. /// 表示OSS的访问控制列表(Access Control List, ACL),
  15. /// 包含了一组为指定被授权者<see cref="IGrantee" />
  16. /// 分配特定权限<see cref="Permission" />的集合。
  17. /// </summary>
  18. public class AccessControlList
  19. {
  20. private readonly Dictionary<Grant, bool> _grants = new Dictionary<Grant, bool>();
  21. /// <summary>
  22. /// 获取所有<see cref="Grant" />实例的枚举。
  23. /// </summary>
  24. public IEnumerable<Grant> Grants
  25. {
  26. get { return _grants.Keys; }
  27. }
  28. /// <summary>
  29. /// 获取或设置所有者。
  30. /// </summary>
  31. public Owner Owner { get; internal set; }
  32. /// <summary>
  33. /// 构造函数。
  34. /// </summary>
  35. internal AccessControlList()
  36. { }
  37. /// <summary>
  38. /// 为指定<see cref="IGrantee" />授予特定的<see cref="Permission" />。
  39. /// 目前只支持被授权者为<see cref="GroupGrantee.AllUsers" />。
  40. /// </summary>
  41. /// <param name="grantee">被授权者。</param>
  42. /// <param name="permission">被授予的权限。</param>
  43. internal void GrantPermission(IGrantee grantee, Permission permission)
  44. {
  45. if (grantee == null)
  46. throw new ArgumentNullException("grantee");
  47. _grants.Add(new Grant(grantee, permission), true);
  48. }
  49. /**
  50. * 取消指定{@link Grantee}已分配的所有权限。
  51. * @param grantee
  52. * 被授权者。目前只支持被授权者为{@link GroupGrantee#AllUsers}。
  53. */
  54. /// <summary>
  55. /// 取消指定<see cref="IGrantee" />已分配的所有权限。
  56. /// </summary>
  57. /// <param name="grantee">被授权者。</param>
  58. internal void RevokeAllPermissions(IGrantee grantee)
  59. {
  60. if (grantee == null)
  61. throw new ArgumentNullException("grantee");
  62. foreach (var e in _grants.Keys)
  63. {
  64. if (e.Grantee == grantee)
  65. {
  66. _grants.Remove(e);
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 返回该对象的字符串表示。
  72. /// </summary>
  73. /// <returns>对象的字符串表示形式</returns>
  74. public override String ToString() {
  75. var grantsBuilder = new StringBuilder();
  76. foreach(var g in Grants)
  77. {
  78. grantsBuilder.Append(g).Append(",");
  79. }
  80. return string.Format(CultureInfo.InvariantCulture,
  81. "[AccessControlList: Owner={0}, Grants={1}]", Owner, grantsBuilder);
  82. }
  83. }
  84. }