public override void CreateOrUpdateOrganization(RmUnifyOrganization organization, Source source)
        {
            using (var context = new UsersContext())
            {
                // Get the school (if it exists)
                School school = (from s in context.Schools
                              where s.RmUnifyOrganizationId == organization.Id
                              select s).SingleOrDefault();

                if (school == null)
                {
                    // School does not exist - create
                    school = new School()
                    {
                        RmUnifyOrganizationId = organization.Id,
                        DisplayName = organization.Name
                    };
                    context.Schools.Add(school);
                    context.SaveChanges();
                }
                else
                {
                    // School exists - update
                    if (school.Deleted != null || school.RmUnifyOrganizationId != organization.Id)
                    {
                        school.Deleted = null;
                        school.RmUnifyOrganizationId = organization.Id;
                        context.SaveChanges();
                    }
                }
            }
        }
        /// <summary>
        /// Create a new organization or update an existing one.
        /// This method will never be called if the organization.Id is null (for example, if the attribute
        /// has not been requested from RM Unify).
        /// If your app stores information about an organization, it should use organization.Id as a key to create a
        /// new organization record or update an existing one.
        /// </summary>
        /// <param name="org">Organization profile</param>
        /// <param name="source">Source of update (sign on or provisioning)</param>
        public override void CreateOrUpdateOrganization(RmUnifyOrganization org, Source source)
        {
            using (var context = new Context())
            {
                School school = (from s in context.Schools
                                 where s.RmUnifyId == org.Id
                                 select s).SingleOrDefault();
                if (school == null)
                {
                    school = new School()
                    {
                        RmUnifyId = org.Id
                    };
                    context.Schools.Add(school);
                }
                school.Name = org.Name;
                school.DfeCode = org.Code;
                school.PostCode = "N/A";
                school.IsRmUnifySchool = true;
                context.SaveChanges();

                // Cache school for next method
                _school = school;
            }
        }
 /// <summary>
 /// Check that a pre-existing organization that has been linked to RM Unify is licenced in your app.
 /// Only necessary if your app supports SSO Connectors (http://dev.rmunify.com/reference/supporting-sso-connector-licensing.aspx).
 /// In this case, the organization has purchased your app outside RM Unify and wishes to connect RM Unify to this
 /// licence. They get an "app establishment key" from you and enter it into RM Unify. This key is passed in to this
 /// method for you to verify the licence.
 /// Called before UpdateLinkedOrganization() if the organization has an SSO Connector to your app.
 /// </summary>
 /// <param name="appEstablishmentKey">App establishment key (as provided by you to the organization)</param>
 /// <param name="organization">Organization profile</param>
 /// <param name="source">Source of update (sign on or provisioning)</param>
 /// <returns>True if organization licensed, false otherwise</returns>
 public override bool IsOrganizationLicensed(string appEstablishmentKey, RmUnifyOrganization organization, Source source)
 {
     using (var context = new Context())
     {
         var school = (from s in context.Schools
                         where s.RmUnifyId == appEstablishmentKey
                         select s).SingleOrDefault();
         if (school == null)
         {
             throw new RmUnifySsoException(RmUnifySsoException.ERRORCODES_INVALIDAPPESTABLISHMENTKEY, "No school with app establishment key: " + appEstablishmentKey);
         }
         return school.Licenced;
     }
 }
 /// <summary>
 /// Update properties of a pre-existing organization that has been linked to RM Unify.
 /// Only necessary if your app supports SSO Connectors (http://dev.rmunify.com/reference/supporting-sso-connector-licensing.aspx)
 /// or the RM Unify user account matching process (http://dev.rmunify.com/reference/supporting-user-account-matching/the-rm-unify-process.aspx).
 /// In this case, the organization has obtained your app outside RM Unify and wishes to connect RM Unify to their
 /// existing establishment in your app. They get an "app establishment key" from you and enter it into RM Unify.
 /// This key is passed in to this method for you to identify the establishment and update it.
 /// Called instead of CreateOrUpdateOrganization() if the organization has an SSO Connector to your app
 /// or has linked the organization as part of the RM Unify user account matching process.
 /// </summary>
 /// <param name="appEstablishmentKey">App establishment key (as provided by you to the organization)</param>
 /// <param name="organization">Organization profile</param>
 /// <param name="source">Source of update (sign on or provisioning)</param>
 public virtual void UpdateLinkedOrganization(string appEstablishmentKey, RmUnifyOrganization organization, Source source)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Check that a pre-existing organization that has been linked to RM Unify is licenced in your app.
 /// Only necessary if your app supports SSO Connectors (http://dev.rmunify.com/reference/supporting-sso-connector-licensing.aspx).
 /// In this case, the organization has purchased your app outside RM Unify and wishes to connect RM Unify to this
 /// licence. They get an "app establishment key" from you and enter it into RM Unify. This key is passed in to this
 /// method for you to verify the licence.
 /// Called before UpdateLinkedOrganization() if the organization has an SSO Connector to your app.
 /// </summary>
 /// <param name="appEstablishmentKey">App establishment key (as provided by you to the organization)</param>
 /// <param name="organization">Organization profile</param>
 /// <param name="source">Source of update (sign on or provisioning)</param>
 /// <returns>True if organization licensed, false otherwise</returns>
 public virtual bool IsOrganizationLicensed(string appEstablishmentKey, RmUnifyOrganization organization, Source source)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Create a new organization or update an existing one.
 /// This method will never be called if the organization.Id is null (for example, if the attribute
 /// has not been requested from RM Unify).
 /// If your app stores information about an organization, it should use organization.Id as a key to create a
 /// new organization record or update an existing one.
 /// </summary>
 /// <param name="organization">Organization profile</param>
 /// <param name="source">Source of update (sign on or provisioning)</param>
 public abstract void CreateOrUpdateOrganization(RmUnifyOrganization organization, Source source);
        /// <summary>
        /// Update properties of a pre-existing organization that has been linked to RM Unify.
        /// Only necessary if your app supports SSO Connectors (http://dev.rmunify.com/reference/supporting-sso-connector-licensing.aspx)
        /// or the RM Unify user account matching process (http://dev.rmunify.com/reference/supporting-user-account-matching/the-rm-unify-process.aspx).
        /// In this case, the organization has obtained your app outside RM Unify and wishes to connect RM Unify to their
        /// existing establishment in your app. They get an "app establishment key" from you and enter it into RM Unify.
        /// This key is passed in to this method for you to identify the establishment and update it.
        /// Called instead of CreateOrUpdateOrganization() if the organization has an SSO Connector to your app
        /// or has linked the organization as part of the RM Unify user account matching process.
        /// </summary>
        /// <param name="appEstablishmentKey">App establishment key (as provided by you to the organization)</param>
        /// <param name="organization">Organization profile</param>
        /// <param name="source">Source of update (sign on or provisioning)</param>
        public override void UpdateLinkedOrganization(string appEstablishmentKey, RmUnifyOrganization organization, Source source)
        {
            using (var context = new Context())
            {
                var school = (from s in context.Schools
                                where s.RmUnifyId == appEstablishmentKey
                                select s).SingleOrDefault();
                if (school == null)
                {
                    throw new RmUnifySsoException(RmUnifySsoException.ERRORCODES_INVALIDAPPESTABLISHMENTKEY, "No school with app establishment key: " + appEstablishmentKey);
                }

                school.Name = organization.Name;
                school.DfeCode = organization.Code;
                school.IsRmUnifySchool = true;
                context.SaveChanges();

                // Cache school for next method
                _school = school;
            }
        }