Exemple #1
0
        internal void GrantAccess(string[] channels, string[] channelGroups, string[] authKeys, bool read, bool write, bool manage, long ttl, PNCallback <PNAccessManagerGrantResult> callback)
        {
            if (string.IsNullOrEmpty(config.SecretKey) || string.IsNullOrEmpty(config.SecretKey.Trim()) || config.SecretKey.Length <= 0)
            {
                throw new MissingMemberException("Invalid secret key");
            }

            if ((channels == null && channelGroups == null) ||
                (channels != null && channelGroups != null && channels.Length == 0 && channelGroups.Length == 0))
            {
                throw new MissingMemberException("Invalid Channels/ChannelGroups");
            }

            List <string> channelList      = new List <string>();
            List <string> channelGroupList = new List <string>();
            List <string> authList         = new List <string>();

            if (channels != null && channels.Length > 0)
            {
                channelList = new List <string>(channels);
                channelList = channelList.Where(ch => !string.IsNullOrEmpty(ch) && ch.Trim().Length > 0).Distinct <string>().ToList();
            }

            if (channelGroups != null && channelGroups.Length > 0)
            {
                channelGroupList = new List <string>(channelGroups);
                channelGroupList = channelGroupList.Where(cg => !string.IsNullOrEmpty(cg) && cg.Trim().Length > 0).Distinct <string>().ToList();
            }

            if (authKeys != null && authKeys.Length > 0)
            {
                authList = new List <string>(authKeys);
                authList = authList.Where(auth => !string.IsNullOrEmpty(auth) && auth.Trim().Length > 0).Distinct <string>().ToList();
            }

            string channelsCommaDelimited      = string.Join(",", channelList.ToArray().OrderBy(x => x).ToArray());
            string channelGroupsCommaDelimited = string.Join(",", channelGroupList.ToArray().OrderBy(x => x).ToArray());
            string authKeysCommaDelimited      = string.Join(",", authList.ToArray().OrderBy(x => x).ToArray());

            IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit);
            Uri request = urlBuilder.BuildGrantAccessRequest(channelsCommaDelimited, channelGroupsCommaDelimited, authKeysCommaDelimited, read, write, manage, ttl);

            RequestState <PNAccessManagerGrantResult> requestState = new RequestState <PNAccessManagerGrantResult>();

            requestState.Channels          = channels;
            requestState.ChannelGroups     = channelGroups;
            requestState.ResponseType      = PNOperationType.PNAccessManagerGrant;
            requestState.PubnubCallback    = callback;
            requestState.Reconnect         = false;
            requestState.EndPointOperation = this;

            string json = UrlProcessRequest <PNAccessManagerGrantResult>(request, requestState, false);

            if (!string.IsNullOrEmpty(json))
            {
                List <object> result = ProcessJsonResponse <PNAccessManagerGrantResult>(requestState, json);
                ProcessResponseCallbacks(result, requestState);
            }
        }