Ejemplo n.º 1
0
        /// <summary>
        /// Called when the user has manually requested to relinquish a relaxed policy.
        /// </summary>
        private void OnRelinquishRelaxedPolicyRequested()
        {
            RelaxedPolicyStatus status = getRelaxedPolicyStatus();

            // Ensure timer is stopped and re-enable categories by simply calling the timer's expiry callback.
            if (status == RelaxedPolicyStatus.Activated)
            {
                var cfg = policyConfiguration.Configuration;

                OnRelaxedPolicyTimerExpired(null);
            }

            // We want to inform the user that there is no relaxed policy in effect currently for this installation.
            if (status == RelaxedPolicyStatus.Deactivated)
            {
                var cfg = policyConfiguration.Configuration;
                ipcServer.NotifyRelaxedPolicyChange(cfg.BypassesPermitted - cfg.BypassesUsed, cfg.BypassDuration, RelaxedPolicyStatus.AlreadyRelinquished);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Notifies all clients of the supplied relaxed policy.Debugrmation.
        /// </summary>
        /// <param name="numPoliciesAvailable">
        /// The number of relaxed policy uses available.
        /// </param>
        /// <param name="policyDuration">
        /// The duration of the relaxed policies.
        /// </param>
        /// <param name="isActive">
        /// Whether or not the relaxed policy is currently active.
        /// </param>
        /// <param name="command">
        /// The relaxed policy command which caused this notification to happen.
        /// If == RelaxedPolicyCommand.Info, ignore.
        /// </param>
        public void NotifyRelaxedPolicyChange(int numPoliciesAvailable, TimeSpan policyDuration, RelaxedPolicyStatus status, string bypassMessage = null)
        {
            var nfo = new RelaxedPolicyInfo(policyDuration, numPoliciesAvailable, status);
            var msg = new RelaxedPolicyMessage(RelaxedPolicyCommand.Info, null, bypassMessage, nfo);

            PushMessage(msg);
        }
Ejemplo n.º 3
0
        public bool RequestRelaxedPolicy(string passcode, out string bypassNotification)
        {
            HttpStatusCode statusCode;
            bool           grantBypass = false;

            if (getRelaxedPolicyStatus() == RelaxedPolicyStatus.Activated)
            {
                bypassNotification = "Relaxed Policy already in effect.";

                var cfg = policyConfiguration.Configuration;
                if (cfg != null)
                {
                    ipcServer.NotifyRelaxedPolicyChange(cfg.BypassesPermitted - cfg.BypassesUsed, cfg.BypassDuration, RelaxedPolicyStatus.Activated, bypassNotification);
                }
                else
                {
                    ipcServer.NotifyRelaxedPolicyChange(0, new TimeSpan(0), RelaxedPolicyStatus.Activated, bypassNotification);
                }

                return(false);
            }

            var parameters = new Dictionary <string, object>();

            if (passcode != null)
            {
                parameters.Add("passcode", passcode);
            }

            byte[] bypassResponse = WebServiceUtil.Default.RequestResource(ServiceResource.BypassRequest, out statusCode, parameters);

            bool useLocalBypassLogic = false;

            bypassNotification = "";

            int bypassesUsed      = 0;
            int bypassesPermitted = 0;

            if (bypassResponse != null)
            {
                if (statusCode == HttpStatusCode.NotFound)
                {
                    // Fallback on local bypass logic if server does not support relaxed policy checks.
                    useLocalBypassLogic = true;
                }

                string jsonString = Encoding.UTF8.GetString(bypassResponse);
                logger.Info("Response received {0}: {1}", statusCode.ToString(), jsonString);

                var bypassObject = JsonConvert.DeserializeObject <RelaxedPolicyResponseObject>(jsonString);

                if (bypassObject.allowed)
                {
                    grantBypass = true;
                }
                else
                {
                    grantBypass        = false;
                    bypassNotification = bypassObject.message;
                }

                bypassesUsed      = bypassObject.used;
                bypassesPermitted = bypassObject.permitted;
            }
            else
            {
                logger.Info("No response detected.");

                useLocalBypassLogic = false;
                grantBypass         = false;
            }

            if (useLocalBypassLogic)
            {
                logger.Info("Using local bypass logic since server does not yet support bypasses.");

                // Start the count down timer.
                if (m_relaxedPolicyExpiryTimer == null)
                {
                    m_relaxedPolicyExpiryTimer = new Timer(OnRelaxedPolicyTimerExpired, null, Timeout.Infinite, Timeout.Infinite);
                }

                enableRelaxedPolicy();

                var cfg = policyConfiguration.Configuration;
                m_relaxedPolicyExpiryTimer.Change(cfg != null ? cfg.BypassDuration : TimeSpan.FromMinutes(5), Timeout.InfiniteTimeSpan);

                DecrementRelaxedPolicy_Local();
            }
            else
            {
                if (grantBypass)
                {
                    logger.Info("Relaxed policy granted.");

                    // Start the count down timer.
                    if (m_relaxedPolicyExpiryTimer == null)
                    {
                        m_relaxedPolicyExpiryTimer = new Timer(OnRelaxedPolicyTimerExpired, null, Timeout.Infinite, Timeout.Infinite);
                    }

                    enableRelaxedPolicy();


                    var cfg = policyConfiguration.Configuration;
                    m_relaxedPolicyExpiryTimer.Change(cfg != null ? cfg.BypassDuration : TimeSpan.FromMinutes(5), Timeout.InfiniteTimeSpan);

                    cfg.BypassesUsed      = bypassesUsed;
                    cfg.BypassesPermitted = bypassesPermitted;

                    DecrementRelaxedPolicy(bypassesUsed, bypassesPermitted, cfg != null ? cfg.BypassDuration : TimeSpan.FromMinutes(5));
                }
                else
                {
                    var cfg = policyConfiguration.Configuration;

                    RelaxedPolicyStatus status = RelaxedPolicyStatus.AllUsed;
                    if (bypassNotification.IndexOf("incorrect passcode", StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        status = RelaxedPolicyStatus.Unauthorized;
                    }

                    ipcServer.NotifyRelaxedPolicyChange(bypassesPermitted - bypassesUsed, cfg != null ? cfg.BypassDuration : TimeSpan.FromMinutes(5), status);
                }
            }

            return(grantBypass);
        }
 /// <summary>
 /// Constructs a new RelaxedPolicyInfo instance.
 /// </summary>
 /// <param name="relaxDuration">
 /// The duration of each relaxed policy use.
 /// </param>
 /// <param name="numberAvailableToday">
 /// The total number of relaxed policies available for the current day.
 /// </param>
 /// <param name="isActive">
 /// Whether or not the relaxed policy is active.
 /// </param>
 /// <param name="command">
 /// The command which caused this info to be sent.
 /// </param>
 public RelaxedPolicyInfo(TimeSpan relaxDuration, int numberAvailableToday, RelaxedPolicyStatus status)
 {
     RelaxDuration        = relaxDuration;
     NumberAvailableToday = numberAvailableToday;
     Status = status;
 }