Exemple #1
0
        /// <summary>
        /// Creates a new user in the given tenant.
        /// </summary>
        /// <param name="user">The user name.</param>
        /// <param name="password">The password.</param>
        /// <param name="tenant">The tenant to create the user in.</param>
        /// <param name="roles">The names of the roles to add the user to.</param>
        public void Create(string user, string password, string tenant, List <string> roles)
        {
            using (Profiler.Measure("UserService.Create"))
            {
                try
                {
                    var role = "";
                    if (roles != null && roles.Count > 0)
                    {
                        role = roles[0];
                    }

                    Tenant.CreateUser(user, password, tenant, role);

                    using (new TenantAdministratorContext(tenant))
                    {
                        var userAccount = GetUserByName(user);
                        if (userAccount == null)
                        {
                            throw new Exception("The expected user does not exist.");
                        }

                        userAccount = userAccount.AsWritable <UserAccount>();

                        if (roles != null)
                        {
                            // first role already handled. process any others.
                            for (var r = 1; r < roles.Count; r++)
                            {
                                var roleName = roles[r];

                                var userRole = CastEntityHelper.GetEntityByField <Role>(new EntityRef(Role.Name_Field), roleName);

                                if (userRole != null)
                                {
                                    userAccount.UserHasRole.Add(userRole);
                                }
                            }
                        }

                        userAccount.Save();
                    }
                }
                catch (Exception)
                {
                    EventLog.Application.WriteError("Failed to create the user {0}", user);
                    throw;
                }
            }
        }
 /// <summary>
 /// Enables a tenant.
 /// </summary>
 /// <param name="name">The name of the tenant to enable.</param>
 public void Enable(string name)
 {
     using (Profiler.Measure("TenantService.Enable"))
     {
         try
         {
             Tenant.EnableTenant(name);
         }
         catch (Exception)
         {
             EventLog.Application.WriteError("Failed to enable tenant {0}", name);
             throw;
         }
     }
 }
        /// <summary>
        /// Deletes an existing tenant of the given name.
        /// </summary>
        /// <param name="name">The name of the tenant.</param>
        public void Delete(string name)
        {
            using (Profiler.Measure("TenantService.Delete"))
            {
                try
                {
                    Tenant.DeleteTenant(name);

                    if (Tenant.GetTenants().Contains(name))
                    {
                        throw new Exception("The tenant still exists.");
                    }
                }
                catch (Exception)
                {
                    EventLog.Application.WriteError("Failed to delete tenant {0}", name);
                    throw;
                }
            }
        }
        /// <summary>
        /// Renames the tenant.
        /// </summary>
        /// <param name="id">The id of the tenant to rename.</param>
        /// <param name="name">The new name to give the tenant.</param>
        public void Rename(long id, string name)
        {
            using (Profiler.Measure("TenantService.Rename"))
            {
                try
                {
                    if (string.IsNullOrEmpty(name))
                    {
                        throw new ArgumentException(@"New tenant name may not be null or empty.", "name");
                    }

                    string old;

                    using (new GlobalAdministratorContext())
                    {
                        var tenant = EntityRepository.Get <TenantModel>(id);
                        if (tenant == null)
                        {
                            throw new Exception("Tenant not found.");
                        }

                        old = tenant.Name;
                    }

                    if (old == null)
                    {
                        throw new Exception("Tenant has no name.");
                    }

                    Tenant.RenameTenant(old, name);
                }
                catch (Exception)
                {
                    EventLog.Application.WriteError("Failed to rename tenant {0}", id);
                    throw;
                }
            }
        }
        /// <summary>
        /// Creates a new tenant given only a name.
        /// </summary>
        /// <param name="name">The name to use for the new tenant.</param>
        public void Create(string name)
        {
            using (Profiler.Measure("TenantService.Create"))
            {
                try
                {
                    Tenant.CreateTenant(name);
                    AppManager.DeployApp(name, Applications.CoreApplicationId.ToString("B"));
                    AppManager.DeployApp(name, Applications.ConsoleApplicationId.ToString("B"));
                    AppManager.DeployApp(name, Applications.CoreDataApplicationId.ToString("B"));

                    if (!Tenant.GetTenants().Contains(name))
                    {
                        throw new Exception("The expected tenant does not exist.");
                    }
                }
                catch (Exception)
                {
                    EventLog.Application.WriteError("Failed to create tenant {0}", name);
                    throw;
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Deletes a user from the given tenant.
        /// </summary>
        /// <param name="user">The user name.</param>
        /// <param name="tenant">The tenant containing the user.</param>
        public void Delete(string user, string tenant)
        {
            using (Profiler.Measure("UserService.DeleteUser"))
            {
                if (user == null)
                {
                    throw new ArgumentNullException("user");
                }

                try
                {
                    long id = 0;
                    using (new TenantAdministratorContext(tenant))
                    {
                        var userAccount = GetUserByName(user);
                        if (userAccount != null)
                        {
                            id = userAccount.Id;
                        }
                    }

                    if (id > 0)
                    {
                        Tenant.DeleteUser(user, tenant);

                        // Can't do this. It's always cached!
                        //if (Entity.Exists(new EntityRef(id)))
                        //    throw new Exception("The user still exists.");
                    }
                }
                catch (Exception)
                {
                    EventLog.Application.WriteError("Failed to delete the user {0}", user);
                    throw;
                }
            }
        }