Пример #1
0
        public async Task InsertRole(KoreRole role)
        {
            var result = await roleManager.CreateAsync(new ApplicationRole
            {
                TenantId = role.TenantId,
                Name     = role.Name
            });

            if (!result.Succeeded)
            {
                string errorMessage = string.Join(Environment.NewLine, result.Errors);
                throw new KoreException(errorMessage);
            }
        }
Пример #2
0
        public virtual async Task <IHttpActionResult> Post(KoreRole entity)
        {
            if (!CheckPermission(KoreWebPermissions.MembershipRolesWrite))
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            entity.TenantId = workContext.CurrentTenant.Id;
            await Service.InsertRole(entity);

            return(Created(entity));
        }
Пример #3
0
        public async Task UpdateRole(KoreRole role)
        {
            string id           = role.Id.ToString();
            var    existingRole = await roleManager.Roles.FirstOrDefaultAsync(x => x.Id == id);

            if (existingRole != null)
            {
                existingRole.Name = role.Name;
                var result = await roleManager.UpdateAsync(existingRole);

                if (!result.Succeeded)
                {
                    string errorMessage = string.Join(Environment.NewLine, result.Errors);
                    throw new KoreException(errorMessage);
                }
            }
        }
Пример #4
0
        public virtual async Task <IHttpActionResult> Delete([FromODataUri] string key)
        {
            if (!CheckPermission(KoreWebPermissions.MembershipRolesWrite))
            {
                return(Unauthorized());
            }

            KoreRole entity = await Service.GetRoleById(key);

            if (entity == null)
            {
                return(NotFound());
            }

            await Service.DeleteRole(key);

            return(StatusCode(HttpStatusCode.NoContent));
        }
Пример #5
0
        public virtual async Task <IHttpActionResult> Patch([FromODataUri] string key, Delta <KoreRole> patch)
        {
            if (!CheckPermission(KoreWebPermissions.MembershipRolesWrite))
            {
                return(Unauthorized());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            KoreRole entity = await Service.GetRoleById(key);

            if (entity == null)
            {
                return(NotFound());
            }

            patch.Patch(entity);

            try
            {
                await Service.UpdateRole(entity);
            }
            catch (DbUpdateConcurrencyException x)
            {
                logger.Value.Error(x.Message, x);

                if (!EntityExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(entity));
        }
Пример #6
0
        private static async Task EnsureMembership(IMembershipService membershipService, IEnumerable <int> tenantIds)
        {
            // We only run this method to ensure that the admin user has been setup as part of the installation process.
            //  If there are any users already in the DB...
            if (await membershipService.GetAllUsersAsQueryable(null).AnyAsync())
            {
                // ... we assume the admin user is one of them. No need for further querying...
                return;
            }

            var dataSettings = EngineContext.Current.Resolve <DataSettings>();

            var adminUser = await membershipService.GetUserByEmail(null, dataSettings.AdminEmail);

            if (adminUser == null)
            {
                await membershipService.InsertUser(
                    new KoreUser
                {
                    TenantId = null,
                    UserName = dataSettings.AdminEmail,
                    Email    = dataSettings.AdminEmail
                },
                    dataSettings.AdminPassword);

                adminUser = await membershipService.GetUserByEmail(null, dataSettings.AdminEmail);

                // TODO: This doesn't work. Gets error like "No owin.Environment item was found in the context."
                //// Confirm User
                //string token = await membershipService.GenerateEmailConfirmationToken(adminUser.Id);
                //await membershipService.ConfirmEmail(adminUser.Id, token);

                KoreRole administratorsRole = null;
                if (adminUser != null)
                {
                    administratorsRole = await membershipService.GetRoleByName(null, KoreWebConstants.Roles.Administrators);

                    if (administratorsRole == null)
                    {
                        await membershipService.InsertRole(new KoreRole
                        {
                            TenantId = null,
                            Name     = KoreWebConstants.Roles.Administrators
                        });

                        administratorsRole = await membershipService.GetRoleByName(null, KoreWebConstants.Roles.Administrators);

                        await membershipService.AssignUserToRoles(null, adminUser.Id, new[] { administratorsRole.Id });
                    }
                }

                if (membershipService.SupportsRolePermissions && administratorsRole != null)
                {
                    var fullAccessPermission = await membershipService.GetPermissionByName(null, StandardPermissions.FullAccess.Name);

                    await membershipService.AssignPermissionsToRole(administratorsRole.Id, new[] { fullAccessPermission.Id });
                }

                dataSettings.AdminPassword = null;
                DataSettingsManager.SaveSettings(dataSettings);
            }

            if (membershipService.SupportsRolePermissions)
            {
                foreach (int tenantId in tenantIds)
                {
                    await membershipService.EnsureAdminRoleForTenant(tenantId);
                }
            }
        }