using LeanCloud.Storage.Internal; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace LeanCloud { /// /// Represents a Role on the LeanCloud server. AVRoles represent groupings /// of s for the purposes of granting permissions (e.g. /// specifying a for a . Roles /// are specified by their sets of child users and child roles, all of which are granted /// any permissions that the parent role has. /// /// Roles must have a name (that cannot be changed after creation of the role), /// and must specify an ACL. /// [AVClassName("_Role")] public class AVRole : AVObject { private static readonly Regex namePattern = new Regex("^[0-9a-zA-Z_\\- ]+$"); /// /// Constructs a new AVRole. You must assign a name and ACL to the role. /// public AVRole() : base() { } /// /// Constructs a new AVRole with the given name. /// /// The name of the role to create. /// The ACL for this role. Roles must have an ACL. public AVRole(string name, AVACL acl) : this() { Name = name; ACL = acl; } /// /// Gets the name of the role. /// [AVFieldName("name")] public string Name { get { return GetProperty("Name"); } set { SetProperty(value, "Name"); } } /// /// Gets the for the s that are /// direct children of this role. These users are granted any privileges that /// this role has been granted (e.g. read or write access through ACLs). You can /// add or remove child users from the role through this relation. /// [AVFieldName("users")] public AVRelation Users { get { return GetRelationProperty("Users"); } } /// /// Gets the for the s that are /// direct children of this role. These roles' users are granted any privileges that /// this role has been granted (e.g. read or write access through ACLs). You can /// add or remove child roles from the role through this relation. /// [AVFieldName("roles")] public AVRelation Roles { get { return GetRelationProperty("Roles"); } } internal override void OnSettingValue(ref string key, ref object value) { base.OnSettingValue(ref key, ref value); if (key == "name") { if (ObjectId != null) { throw new InvalidOperationException( "A role's name can only be set before it has been saved."); } if (!(value is string)) { throw new ArgumentException("A role's name must be a string.", "value"); } if (!namePattern.IsMatch((string)value)) { throw new ArgumentException( "A role's name can only contain alphanumeric characters, _, -, and spaces.", "value"); } } } /// /// Gets a over the Role collection. /// public static AVQuery Query { get { return new AVQuery(); } } } }