Пример #1
0
        public async Task UpdateRole(
            ISiteRole role,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ThrowIfDisposed();
            cancellationToken.ThrowIfCancellationRequested();
            if (role == null)
            {
                throw new ArgumentException("role cannot be null");
            }
            if (role.SiteId == Guid.Empty)
            {
                throw new ArgumentException("SiteId must be provided");
            }
            if (role.Id == Guid.Empty)
            {
                throw new ArgumentException("Id must be provided");
            }

            var siteRole = SiteRole.FromISiteRole(role);

            bool tracking = dbContext.ChangeTracker.Entries <SiteRole>().Any(x => x.Entity.Id == siteRole.Id);

            if (!tracking)
            {
                dbContext.Roles.Update(siteRole);
            }

            int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken)
                               .ConfigureAwait(false);
        }
Пример #2
0
        public async Task CreateRole(
            ISiteRole role,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ThrowIfDisposed();
            cancellationToken.ThrowIfCancellationRequested();

            if (role == null)
            {
                throw new ArgumentException("role cannot be null");
            }
            if (role.SiteId == Guid.Empty)
            {
                throw new ArgumentException("SiteId must be provided");
            }
            if (role.Id == Guid.Empty)
            {
                throw new ArgumentException("Id must be provided");
            }

            //await EnsureProjectId().ConfigureAwait(false);
            var projectId = role.SiteId.ToString();

            var siteRole = SiteRole.FromISiteRole(role);

            if (siteRole.NormalizedRoleName.Length == 0)
            {
                siteRole.NormalizedRoleName = siteRole.RoleName;
            }
            await roleCommands.CreateAsync(
                projectId,
                siteRole.Id.ToString(),
                siteRole,
                cancellationToken).ConfigureAwait(false);
        }
Пример #3
0
        /// <summary>
        /// Persists a new instance of Role. Returns true on success.
        /// when a role is created displayname corresponds to rolename
        /// but rolename can never change since it is used in a cookies and coded
        /// into security checks in some cases
        /// so subsequent changes to rolename really only effect displayname
        /// ie for localization or customization
        /// to really change a rolename you can delete the role and create a new one with the desired name
        /// some specific required system roles (Admin, Content Administrators)
        /// are also not allowed to be deleted
        /// </summary>
        /// <returns></returns>
        public async Task CreateRole(
            ISiteRole role,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ThrowIfDisposed();
            cancellationToken.ThrowIfCancellationRequested();

            if (role == null)
            {
                throw new ArgumentException("role cannot be null");
            }
            if (role.SiteId == Guid.Empty)
            {
                throw new ArgumentException("SiteId must be provided");
            }
            if (role.Id == Guid.Empty)
            {
                throw new ArgumentException("Id must be provided");
            }

            var siteRole = SiteRole.FromISiteRole(role);

            if (siteRole.NormalizedRoleName.Length == 0)
            {
                siteRole.NormalizedRoleName = siteRole.RoleName;
            }
            dbContext.Roles.Add(siteRole);

            int rowsAffected = await dbContext.SaveChangesAsync(cancellationToken)
                               .ConfigureAwait(false);
        }
        public async Task <IActionResult> RoleEdit(RoleViewModel model, int returnPageNumber = 1)
        {
            ISiteContext selectedSite;

            // only server admin site can edit other sites settings
            if ((model.SiteId != Guid.Empty) && (model.SiteId != siteManager.CurrentSite.Id) && (siteManager.CurrentSite.IsServerAdminSite))
            {
                selectedSite = await siteManager.Fetch(model.SiteId) as ISiteContext;

                ViewData["Title"] = string.Format(CultureInfo.CurrentUICulture, sr["{0} - Edit Role"], selectedSite.SiteName);
            }
            else
            {
                selectedSite      = siteManager.CurrentSite;
                ViewData["Title"] = sr["Edit Role"];
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string successFormat;
            var    role = SiteRole.FromISiteRole(model);

            if (model.Id == Guid.Empty)
            {
                var exists = await RoleManager.RoleExistsAsync(model.RoleName);

                if (exists)
                {
                    ModelState.AddModelError("roleerror", sr["The role name is already in use."]);
                    return(View(model));
                }

                role.SiteId   = selectedSite.Id;
                successFormat = sr["The role <b>{0}</b> was successfully created."];
                await RoleManager.CreateAsync(role);
            }
            else
            {
                successFormat = sr["The role <b>{0}</b> was successfully updated."];
                await RoleManager.UpdateAsync(role);
            }

            this.AlertSuccess(string.Format(successFormat, role.RoleName), true);

            return(RedirectToAction("Index", new { siteId = selectedSite.Id, pageNumber = returnPageNumber }));
        }