private void SetProdType(bool value, DisclaimerDataType d)
 {
     if (value) {
         // Set flag
         ProductionTypes |= d;
     } else {
         // Unset flag
         ProductionTypes &= ~d;
     }
 }
        /// <summary>
        /// Associates a property with the user account specified by actingUserId.
        /// </summary>
        /// <param name="actualUserId">(int) The actual logged-in user.</param>
        /// <param name="actingUserId">
        ///		(int) The "act-as" user (may be the same as the logged-in user; 
        ///		actualUser must have privileges to act-as if this ID is different).
        ///		This is the user account the property will be associated with.
        ///	</param>
        /// <param name="role">(PropertyRole) The acting user's role in relation to the property.</param>
        /// <param name="parcelId">(string) The parcel ID (PropertyNumber) corresponding to the property in appraisal roll records.</param>
        /// <param name="county">(string) The county of the </param>
        /// <param name="productionTypes"></param>
        /// <returns></returns>
        public JsonResult AddProperty(string authTicket, int actualUserId, int actingUserId, PropertyRole role, string parcelId, string county, DisclaimerDataType productionTypes)
        {
            var errors = new List<string>();
            Func<JsonResult> jsonStatus = () => {
                return Json(new JsonResponse(errors.Count == 0, errors.ToArray()));
            };

            // 0. Verify that all required data is present.
            if (actualUserId < 1) {
                errors.Add("Parameter 'actualUserId' is required and must be greater than 0.");
            }
            if (actingUserId < 1) {
                errors.Add("Parameter 'actingUserId' is required and must be greater than 0.");
            }
            if (string.IsNullOrWhiteSpace(parcelId)) {
                errors.Add("Parameter 'parcelId' is required and cannot be blank.");
            }
            if (string.IsNullOrWhiteSpace(county)) {
                errors.Add("Parameter 'county' is required and cannot be blank.");
            }
            if (!Enum.IsDefined(typeof(PropertyRole), role)) {
                errors.Add("Invalid property role: " + role.ToString());
            }
            // Check for validity of production types - this is a flags enum,
            // so valid values are anything > 0 and < (sum of all values)
            var maxVal = Enum.GetValues(typeof(DisclaimerDataType)).Cast<int>().Sum();
            if (productionTypes < 0 || (int)productionTypes > maxVal || (productionTypes == 0 && role != PropertyRole.installer)) {
                errors.Add("Invalid production type: " + productionTypes.ToString());
            }

            if (errors.Count > 0) {
                return jsonStatus();
            }

            var udalc = new UserDalc();
            User actualUser = GetUserFromAuthTicket(authTicket);
            User actingUser = udalc.GetUser(actingUserId);
            if (actualUser == null) {
                errors.Add("Unable to find user account based on provided auth ticket.");
            }
            if (actingUser == null) {
                errors.Add("Unable to find user account corresponding to actingUserId == " + actingUserId.ToString());
            }
            if (errors.Count > 0) {
                return jsonStatus();
            }

            if (actualUser.Id != actualUserId) {
                // Bizarre - the auth ticket is not for the specified user id.
                errors.Add("Unauthorized action: The specified authentication ticket does not match the provided actual user ID.");
                return jsonStatus();
            }

            // 1. Ensure actual user has permission to pose as acting user
            if (actualUserId != actingUserId && !actualUser.IsAdmin) {
                errors.Add("Unauthorized action: You do not have permission to act for that user.");
                return jsonStatus();
            }

            var propDalc = new PropertyDalc();

            // 2. Verify that the property matches values in AppraisalRolls.
            //    Also check to ensure there is only one property matching this parcel id
            //		and county. (This is in response to a bug in production where there
            //		where many parcelIds of 0 in Cochran county; without this check
            //		some hundreds of records would be associated with the user account.)
            int propertyCount = propDalc.GetPropertyCount(parcelId, county);

            if (propertyCount == 0) {
                errors.Add(string.Format("Unable to find a matching appraisal roll record for parcel ID '{0}', county '{1}'", parcelId, county));
                return jsonStatus();
            } else if (propertyCount > 1) {
                errors.Add(string.Format("Multiple ({0}) records found for parcel ID '{1}', county '{2}'. Cannot add property when duplicates exist.", propertyCount, parcelId, county));
                return jsonStatus();
            }

            // 3. If the property has already been associated with the user account,
            //		return an error message to that effect.
            if (propDalc.IsPropertyAssociated(actingUserId, parcelId, county)) {
                errors.Add("The property is already associated with your account. If you wish to change roles, please delete the existing property from your account and add it again with the different role.");
                return jsonStatus();
            }

            // 4. Create the association.
            propDalc.AssociateProperty(actingUser, new Property(parcelId, county, ""), role, true, productionTypes, false);

            return jsonStatus();
        }
 // Most of these are here strictly for convenience using them in the view,
 // particularly the bool properties (for checkboxes).
 private bool GetProdType(DisclaimerDataType d)
 {
     return (ProductionTypes & d) == d;
 }
        public JsonResult ChangeUserRole(string authTicket, int actingUserId, int actualUserId, PropertyRole? role, string parcelId, string county, DisclaimerDataType? productionTypes)
        {
            try {
                List<string> errors = new List<string>();
                Func<JsonResult> jsonStatus = () => {
                    return Json(new JsonResponse(errors.Count == 0, errors.ToArray()));
                };

                // 0. Verify that all required data is present.
                if (actualUserId < 1) {
                    errors.Add("Parameter 'actualUserId' is required and must be greater than 0.");
                }
                if (actingUserId < 1) {
                    errors.Add("Parameter 'actingUserId' is required and must be greater than 0.");
                }
                if (string.IsNullOrWhiteSpace(parcelId)) {
                    errors.Add("Parameter 'parcelId' is required and cannot be blank.");
                }
                if (string.IsNullOrWhiteSpace(county)) {
                    errors.Add("Parameter 'county' is required and cannot be blank.");
                }

                if (!role.HasValue) {
                    // Default to operator
                    role = PropertyRole.authorized_producer;
                }
                if (!Enum.IsDefined(typeof(PropertyRole), role)) {
                    errors.Add("Invalid property role: " + role.ToString());
                }

                if (!productionTypes.HasValue) {
                    productionTypes = DisclaimerDataType.agriculture;
                }
                // Check for validity of production types - this is a flags enum,
                // so valid values are anything > 0 and < (sum of all values)
                var maxVal = Enum.GetValues(typeof(DisclaimerDataType)).Cast<int>().Sum();
                if (productionTypes < 0 || (int)productionTypes > maxVal || (productionTypes == 0 && role != PropertyRole.installer)) {
                    errors.Add("Invalid production type: " + productionTypes.ToString());
                }

                if (errors.Count > 0) {
                    return jsonStatus();
                }

                var udalc = new UserDalc();
                User actualUser = GetUserFromAuthTicket(authTicket);
                User actingUser = udalc.GetUser(actingUserId);
                if (actualUser == null) {
                    errors.Add("Unable to find user account based on provided auth ticket.");
                }
                if (actingUser == null) {
                    errors.Add("Unable to find user account corresponding to actingUserId == " + actingUserId.ToString());
                }
                if (errors.Count > 0) {
                    return jsonStatus();
                }

                if (actualUser.Id != actualUserId) {
                    // Bizarre - the auth ticket is not for the specified user id.
                    errors.Add("Unauthorized action: The specified authentication ticket does not match the provided actual user ID.");
                    return jsonStatus();
                }

                // 1. Ensure actual user has permission to pose as acting user
                if (actualUserId != actingUserId && !actualUser.IsAdmin) {
                    errors.Add("Unauthorized action: You do not have permission to act for that user.");
                    return jsonStatus();
                }

                var propDalc = new PropertyDalc();

                // 2. Verify that the property matches values in AppraisalRolls.
                if (!propDalc.DoesPropertyExist(parcelId, county)) {
                    errors.Add(string.Format("Unable to find a matching appraisal roll record for parcel ID '{0}', county '{1}'", parcelId, county));
                    return jsonStatus();
                }

                // 3. If the property has not been associated with the user account,
                //		return an error message to that effect.
                int clientPropertyId;
                if (!propDalc.IsPropertyAssociated(actingUserId, parcelId, county, out clientPropertyId)) {
                    errors.Add("The specified property is not associated with your account. Please first add the property to your account.");
                    return jsonStatus();
                }

                propDalc.ChangePropertyRoleAndProductionType(actingUser, clientPropertyId, role.Value, productionTypes.Value);

                return jsonStatus();
            } catch (Exception ex) {
                return Json(new JsonResponse(false, ex.Message));
            }
        }