Beispiel #1
0
        /// <summary>
        /// Renames the tenant.
        /// </summary>
        /// <param name="tenantName">Name of the tenant.</param>
        /// <param name="newName">The new name.</param>
        /// <exception cref="EntityNotFoundException"></exception>
        public static void RenameTenant(string tenantName, string newName)
        {
            using (new GlobalAdministratorContext( ))
            {
                /////
                // Check for existing tenant
                /////
                Tenant tenant = Find(tenantName);

                if (tenant == null)
                {
                    throw new EntityNotFoundException(string.Format(@"Tenant '{0}' not found.", tenantName));
                }

                tenant = tenant.AsWritable <Tenant>( );

                tenant.Name = newName;
                tenant.Save( );

                /////
                // Invalidate the tenant name to id cache
                /////
                TenantIdCache.Remove(tenantName);

                TenantNameCache.Remove(tenant.Id);
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Finds the ID of the tenant associated with the specified name.
        /// </summary>
        /// <param name="tenantName">A string containing the name of the tenant to search for.</param>
        /// <param name="throwIfMissing">if set to <c>true</c> throws an exception if the tenant name cannot be found.</param>
        /// <returns>
        ///     An object that represents the specified tenant; otherwise -1 if the tenant cannot be found.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">tenantName</exception>
        public static long GetTenantId(string tenantName, bool throwIfMissing = false)
        {
            if (String.IsNullOrEmpty(tenantName))
            {
                throw new ArgumentNullException("tenantName");
            }

            if (tenantName.Equals(SpecialStrings.GlobalTenant, StringComparison.CurrentCultureIgnoreCase))
            {
                return(0);
            }

            /////
            // Check tenant ID cache
            /////
            long tenantId;

            if (TenantIdCache.TryGetValue(tenantName, out tenantId))
            {
                return(tenantId);
            }

            using (new GlobalAdministratorContext( ))
            {
                /////
                // Find tenant by name
                /////
                Tenant tenant = Entity.GetByName <Tenant>(tenantName).FirstOrDefault( );

                if (tenant == null)
                {
                    if (throwIfMissing)
                    {
                        throw new Exception("No tenants found with name " + tenantName);
                    }

                    return(-1);
                }

                /////
                // Cache and return
                /////
                TenantIdCache [tenantName] = tenant.Id;

                return(tenant.Id);
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Finds the tenant associated with the specified name.
        /// </summary>
        /// <param name="tenantName">
        ///     A string containing the name of the tenant to search for.
        /// </param>
        /// <returns>
        ///     An object that represents the specified tenant; otherwise null if the tenant cannot be found.
        /// </returns>
        public static Tenant Find(string tenantName)
        {
            if (String.IsNullOrEmpty(tenantName))
            {
                throw new ArgumentException("The specified tenantName parameter is invalid.");
            }

            Tenant tenant = FindByName(tenantName);

            if (tenant == null)
            {
                TenantIdCache.Remove(tenantName);

                tenant = FindByName(tenantName);
            }

            return(tenant);
        }
Beispiel #4
0
        /// <summary>
        ///     Deletes the tenant from the entity model.
        /// </summary>
        /// <param name="tenantId">The tenant id.</param>
        public static void DeleteTenant(long tenantId)
        {
            EventLog.Application.WriteWarning("Deleting tenant " + tenantId);

            long userId;

            RequestContext.TryGetUserId(out userId);

            using (DatabaseContext ctx = DatabaseContext.GetContext( ))
            {
                // Delete the tenant entity instance itself.
                // Note: this is stored in the root tenant
                using (new AdministratorContext( ))
                {
                    Entity.Delete(tenantId);
                }

                // Delete the data
                using (DatabaseContextInfo.SetContextInfo($"Delete tenant {tenantId}"))
                    using (IDbCommand command = ctx.CreateCommand("spDeleteTenant", CommandType.StoredProcedure))
                    {
                        ctx.AddParameter(command, "@tenantId", DbType.Int64, tenantId);
                        command.AddParameter("@context", DbType.AnsiString, DatabaseContextInfo.GetMessageChain(userId));

                        command.ExecuteNonQuery( );
                    }
            }

            /////
            // Remove the cached tenant id.
            /////
            List <string> matchingTenants = TenantIdCache.Where(pair => pair.Value == tenantId).Select(pair => pair.Key).ToList( );

            foreach (string matchingTenant in matchingTenants)
            {
                TenantIdCache.Remove(matchingTenant);
            }

            TenantNameCache.Remove(tenantId);

            EventLog.Application.WriteWarning("Deleted tenant " + tenantId);
        }
Beispiel #5
0
 /// <summary>
 ///     Flushes this instance.
 /// </summary>
 public static void Flush( )
 {
     TenantIdCache.Clear( );
     TenantNameCache.Clear();
 }