Inheritance: ISiteRole
Ejemplo n.º 1
0
        public static SiteRole BuildContentAdminsRole()
        {
            var role = new SiteRole();
            role.RoleName = "Content Administrators";
            role.NormalizedRoleName = role.RoleName.ToUpperInvariant();

            return role;
        }
Ejemplo n.º 2
0
        public static SiteRole BuildAuthenticatedRole()
        {
            var role = new SiteRole();
            role.RoleName = "Authenticated Users";
            role.NormalizedRoleName = role.RoleName.ToUpperInvariant();

            return role;
        }
Ejemplo n.º 3
0
        public static SiteRole FromISiteRole(ISiteRole i)
        {
            SiteRole r = new SiteRole();
            r.RoleName = i.RoleName;
            r.MemberCount = i.MemberCount;
            r.Id = i.Id;
            r.NormalizedRoleName = i.NormalizedRoleName;
            r.SiteId = i.SiteId;
           // r.ConcurrencyStamp = i.ConcurrencyStamp;


            return r;
        }
Ejemplo n.º 4
0
        public static SiteRole FromISiteRole(ISiteRole i)
        {
            SiteRole r = new SiteRole();

            r.RoleName           = i.RoleName;
            r.MemberCount        = i.MemberCount;
            r.Id                 = i.Id;
            r.NormalizedRoleName = i.NormalizedRoleName;
            r.SiteId             = i.SiteId;
            // r.ConcurrencyStamp = i.ConcurrencyStamp;


            return(r);
        }
Ejemplo n.º 5
0
        public static SiteRole FromISiteRole(ISiteRole i)
        {
            SiteRole r = new SiteRole();
            r.DisplayName = i.DisplayName;
            r.MemberCount = i.MemberCount;
            r.RoleGuid = i.RoleGuid;
            r.RoleId = i.RoleId;
            r.RoleName = i.RoleName;
            r.SiteGuid = i.SiteGuid;
            r.SiteId = i.SiteId;


            return r;
        }
Ejemplo n.º 6
0
        public static SiteRole FromISiteRole(ISiteRole i)
        {
            SiteRole r = new SiteRole();

            r.DisplayName = i.DisplayName;
            r.MemberCount = i.MemberCount;
            r.RoleGuid    = i.RoleGuid;
            r.RoleId      = i.RoleId;
            r.RoleName    = i.RoleName;
            r.SiteGuid    = i.SiteGuid;
            r.SiteId      = i.SiteId;


            return(r);
        }
Ejemplo n.º 7
0
        public static SiteRole FromISiteRole(ISiteRole i)
        {
            SiteRole r = new SiteRole
            {
                RoleName           = i.RoleName,
                MemberCount        = i.MemberCount,
                Id                 = i.Id,
                NormalizedRoleName = i.NormalizedRoleName,
                SiteId             = i.SiteId
            };

            // r.ConcurrencyStamp = i.ConcurrencyStamp;


            return(r);
        }
Ejemplo n.º 8
0
        public async Task<IList<ISiteRole>> GetRolesBySite(
            int siteId,
            string searchInput,
            int pageNumber,
            int pageSize)
        {
            IList<ISiteRole> roles = new List<ISiteRole>();
            using (DbDataReader reader = await dbRoles.GetPage(siteId, searchInput, pageNumber, pageSize))
            {
                while (reader.Read())
                {
                    SiteRole role = new SiteRole();
                    role.LoadFromReader(reader);
                    role.MemberCount = Convert.ToInt32(reader["MemberCount"]);

                    roles.Add(role);
                }
            }

            return roles;

        }
Ejemplo n.º 9
0
        public async Task<ISiteRole> FetchRole(int siteId, string roleName)
        {
            SiteRole role = null;

            using (DbDataReader reader = await dbRoles.GetByName(siteId, roleName))
            {
                if (reader.Read())
                {
                    role = new SiteRole();
                    role.LoadFromReader(reader);
                }
            }

            return role;

        }
Ejemplo n.º 10
0
        //public int GetRoleMemberCount(int roleId)
        //{
        //    // TODO: implement actual select count from db
        //    // this is works but is not ideal
        //    int count = 0;
        //    using (DbDataReader reader = dbRoles.GetRoleMembers(roleId))
        //    {
        //        while (reader.Read())
        //        {
        //            count += 1;
        //        }
        //    }

        //    return count;

        //}

        public async Task<ISiteRole> FetchRole(int roleId)
        {
            using (DbDataReader reader = await dbRoles.GetById(roleId))
            {
                if (reader.Read())
                {
                    SiteRole role = new SiteRole();
                    role.LoadFromReader(reader);
                    return role;
                }
            }

            return null;
        }
Ejemplo n.º 11
0
        public async Task<bool> EnsureRequiredRoles(ISiteSettings site)
        {
            bool result = true;

            bool exists = await userRepo.RoleExists(site.SiteId, "Admins");

            if(!exists)
            {
                SiteRole adminRole = new SiteRole();
                adminRole.DisplayName = "Admins";
                //adminRole.DisplayName = "Administrators";
                adminRole.SiteId = site.SiteId;
                adminRole.SiteGuid = site.SiteGuid;
                result = await userRepo.SaveRole(adminRole);
                adminRole.DisplayName = "Administrators";
                result = await userRepo.SaveRole(adminRole);
            }

            exists = await userRepo.RoleExists(site.SiteId, "Role Admins");

            if (!exists)
            {
                SiteRole roleAdminRole = new SiteRole();
                roleAdminRole.DisplayName = "Role Admins";
                roleAdminRole.SiteId = site.SiteId;
                roleAdminRole.SiteGuid = site.SiteGuid;
                result = await userRepo.SaveRole(roleAdminRole);

                roleAdminRole.DisplayName = "Role Administrators";
                result = await userRepo.SaveRole(roleAdminRole);
            }

            exists = await userRepo.RoleExists(site.SiteId, "Content Administrators");

            if (!exists)
            {
                SiteRole contentAdminRole = new SiteRole();
                contentAdminRole.DisplayName = "Content Administrators";
                contentAdminRole.SiteId = site.SiteId;
                contentAdminRole.SiteGuid = site.SiteGuid;
                result = await userRepo.SaveRole(contentAdminRole);
            }

            exists = await userRepo.RoleExists(site.SiteId, "Authenticated Users");

            if (!exists)
            {
                SiteRole authenticatedUserRole = new SiteRole();
                authenticatedUserRole.DisplayName = "Authenticated Users";
                authenticatedUserRole.SiteId = site.SiteId;
                authenticatedUserRole.SiteGuid = site.SiteGuid;
                result = await userRepo.SaveRole(authenticatedUserRole);
            }

            

            return result;

        }
Ejemplo n.º 12
0
        public async Task<IList<ISiteRole>> GetRolesBySite(
            int siteId,
            string searchInput,
            int pageNumber,
            int pageSize,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            IList<ISiteRole> roles = new List<ISiteRole>();
            using (DbDataReader reader = await dbRoles.GetPage(
                siteId, 
                searchInput, 
                pageNumber, 
                pageSize,
                cancellationToken))
            {
                while (reader.Read())
                {
                    SiteRole role = new SiteRole();
                    role.LoadFromReader(reader);
                    role.MemberCount = Convert.ToInt32(reader["MemberCount"]);

                    roles.Add(role);
                }
            }

            return roles;

        }
Ejemplo n.º 13
0
        public async Task<ISiteRole> FetchRole(
            int siteId, 
            string roleName, 
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            SiteRole role = null;

            using (DbDataReader reader = await dbRoles.GetByName(
                siteId, 
                roleName,
                cancellationToken))
            {
                if (reader.Read())
                {
                    role = new SiteRole();
                    role.LoadFromReader(reader);
                }
            }

            return role;

        }
Ejemplo n.º 14
0
        public async Task<ISiteRole> FetchRole(
            int roleId, 
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (DbDataReader reader = await dbRoles.GetById(roleId, cancellationToken))
            {
                if (reader.Read())
                {
                    SiteRole role = new SiteRole();
                    role.LoadFromReader(reader);
                    return role;
                }
            }

            return null;
        }
Ejemplo n.º 15
0
        public async Task<IList<ISiteRole>> GetRolesBySite(
            int siteId,
            string searchInput,
            int pageNumber,
            int pageSize)
        {
            if (multiTenantOptions.UseRelatedSitesMode) { siteId = multiTenantOptions.RelatedSiteId; }

            IList<ISiteRole> roles = new List<ISiteRole>();
            using (DbDataReader reader = dbRoles.GetPage(siteId, searchInput, pageNumber, pageSize))
            {
                while (reader.Read())
                {
                    SiteRole role = new SiteRole();
                    role.LoadFromReader(reader);
                    role.MemberCount = Convert.ToInt32(reader["MemberCount"]);

                    roles.Add(role);
                }
            }

            return roles;

        }
Ejemplo n.º 16
0
        public async Task<ISiteRole> FetchRole(int siteId, string roleName)
        {
            if (multiTenantOptions.UseRelatedSitesMode) { siteId = multiTenantOptions.RelatedSiteId; }
            SiteRole role = null;

            using (DbDataReader reader = await dbRoles.GetByName(siteId, roleName))
            {
                while (reader.Read())
                {
                    role = new SiteRole();
                    role.LoadFromReader(reader);
                }
            }

            return role;

        }
Ejemplo n.º 17
0
        public IList<ISiteRole> GetRolesUserIsNotIn(
            int siteId,
            int userId)
        {
            if (multiTenantOptions.UseRelatedSitesMode) { siteId = multiTenantOptions.RelatedSiteId; }
            IList<ISiteRole> roles = new List<ISiteRole>();
            using (DbDataReader reader = dbRoles.GetRolesUserIsNotIn(siteId, userId))
            {
                SiteRole role = new SiteRole();
                role.LoadFromReader(reader);

                roles.Add(role);
            }
            return roles;
        }
Ejemplo n.º 18
0
        private static async Task EnsureData(
            CoreDbContext db
            )
        {
            int rowsAffected = 0;


            int count = await db.Countries.CountAsync<GeoCountry>();
            if(count == 0)
            {
                foreach(GeoCountry c in cloudscribe.Core.Models.InitialData.BuildCountryList())
                {
                    db.Countries.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }
            
            count = await db.States.CountAsync<GeoZone>();
            if (count == 0)
            {
                foreach (GeoZone c in cloudscribe.Core.Models.InitialData.BuildStateList())
                {
                    db.States.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }

            

            count = await db.Languages.CountAsync<Language>();
            if (count == 0)
            {
                foreach (Language c in cloudscribe.Core.Models.InitialData.BuildLanguageList())
                {
                    db.Languages.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }

            count = await db.Currencies.CountAsync<Currency>();
            if (count == 0)
            {
                foreach (Currency c in cloudscribe.Core.Models.InitialData.BuildCurrencyList())
                {
                    db.Currencies.Add(c);
                }

                rowsAffected = await db.SaveChangesAsync();
            }

                
            count = await db.Sites.CountAsync<SiteSettings>();
            if (count == 0)
            {
                // create first site
                SiteSettings newSite = new SiteSettings();
                newSite.SiteId = 0;
                newSite.SiteGuid = Guid.NewGuid();
                newSite.SiteName = "Sample Site";
                newSite.IsServerAdminSite = true;

                newSite.Layout = "Default_Layout.cshtml";

                newSite.AllowNewRegistration = true;
                newSite.AllowUserFullNameChange = false;
                newSite.AutoCreateLdapUserOnFirstLogin = true;
                newSite.ReallyDeleteUsers = true;
                newSite.LdapPort = 389;
                newSite.LdapRootDN = string.Empty;
                newSite.LdapServer = string.Empty;
                newSite.UseEmailForLogin = true;
                newSite.UseLdapAuth = false;
                newSite.UseSecureRegistration = false;
                newSite.UseSslOnAllPages = false;


                //0 = clear, 1= hashed, 2= encrypted
                //newSite.PasswordFormat = 1;

                newSite.RequiresQuestionAndAnswer = false;
                newSite.MaxInvalidPasswordAttempts = 10;
                newSite.PasswordAttemptWindowMinutes = 5;
                newSite.MinReqNonAlphaChars = 0;
                newSite.MinRequiredPasswordLength = 7;

                db.Sites.Add(newSite);
                
                rowsAffected = await db.SaveChangesAsync();
                   
            }

            // ensure roles
            count = await db.Roles.CountAsync<SiteRole>();
            if (count == 0)
            {
                SiteSettings site = await db.Sites.SingleOrDefaultAsync<SiteSettings>(
                    s => s.SiteId > 0 && s.IsServerAdminSite == true);

                if(site != null)
                {
                    SiteRole adminRole = new SiteRole();
                    adminRole.RoleId = 0;
                    adminRole.RoleGuid = Guid.NewGuid();
                    adminRole.RoleName = "Admins";
                    adminRole.DisplayName = "Administrators";
                    adminRole.SiteId = site.SiteId;
                    adminRole.SiteGuid = site.SiteGuid;
                    db.Roles.Add(adminRole);
                    //rowsAffected = await db.SaveChangesAsync();
                        
                    SiteRole roleAdminRole = new SiteRole();
                    roleAdminRole.RoleId = 0;
                    roleAdminRole.RoleGuid = Guid.NewGuid();
                    roleAdminRole.RoleName = "Role Admins";
                    roleAdminRole.DisplayName = "Role Administrators";
                    roleAdminRole.SiteId = site.SiteId;
                    roleAdminRole.SiteGuid = site.SiteGuid;
                    db.Roles.Add(roleAdminRole);
                    //rowsAffected = await db.SaveChangesAsync();
                    
                    SiteRole contentAdminRole = new SiteRole();
                    contentAdminRole.RoleId = 0;
                    contentAdminRole.RoleGuid = Guid.NewGuid();
                    contentAdminRole.RoleName = "Content Administrators";
                    contentAdminRole.DisplayName = "Content Administrators";
                    contentAdminRole.SiteId = site.SiteId;
                    contentAdminRole.SiteGuid = site.SiteGuid;
                    db.Roles.Add(contentAdminRole);

                    SiteRole authenticatedUserRole = new SiteRole();
                    authenticatedUserRole.RoleId = 0;
                    authenticatedUserRole.RoleGuid = Guid.NewGuid();
                    authenticatedUserRole.RoleName = "Authenticated Users";
                    authenticatedUserRole.DisplayName = "Authenticated Users";
                    authenticatedUserRole.SiteId = site.SiteId;
                    authenticatedUserRole.SiteGuid = site.SiteGuid;
                    db.Roles.Add(authenticatedUserRole);

                    
                    rowsAffected = await db.SaveChangesAsync();
                    
                   
                }

            }

            // ensure admin user
            count = await db.Users.CountAsync<SiteUser>();
            
            if (count == 0)
            {
                SiteSettings site = await db.Sites.SingleOrDefaultAsync<SiteSettings>(
                    s => s.SiteId > 0 && s.IsServerAdminSite == true);
                    
                if (site != null)
                {
                    SiteRole role
                        = await db.Roles.SingleOrDefaultAsync(
                            x => x.SiteId == site.SiteId && x.RoleName == "Admins");

                    if(role != null)
                    {
                        SiteUser adminUser = new SiteUser();
                        adminUser.SiteId = site.SiteId;
                        adminUser.SiteGuid = site.SiteGuid;
                        adminUser.Email = "*****@*****.**";
                        adminUser.LoweredEmail = adminUser.Email;
                        adminUser.DisplayName = "Admin";
                        adminUser.UserName = "******";
                        adminUser.UserId = 0;
                        adminUser.UserGuid = Guid.NewGuid();

                        adminUser.EmailConfirmed = true;
                        adminUser.AccountApproved = true;

                        // clear text password will be hashed upon login
                        // this format allows migrating from mojoportal
                        adminUser.PasswordHash = "admin||0"; //pwd/salt/format 

                        db.Users.Add(adminUser);
                        
                        rowsAffected = await db.SaveChangesAsync();
                        
                        if(rowsAffected > 0 && adminUser.UserId > -1)
                        {
                            UserRole ur = new UserRole();
                            ur.Id = 0;
                            ur.RoleGuid = role.RoleGuid;
                            ur.RoleId = role.RoleId;
                            ur.UserGuid = adminUser.UserGuid;
                            ur.UserId = adminUser.UserId;

                            db.UserRoles.Add(ur);
                            rowsAffected = await db.SaveChangesAsync();

                        }
                    }

                }

            }
            
        }
Ejemplo n.º 19
0
        public async Task<bool> CreateRequiredRolesAndAdminUser(
            SiteSettings site)
        {

            SiteRole adminRole = new SiteRole();
            adminRole.DisplayName = "Admins";
            //adminRole.DisplayName = "Administrators";
            adminRole.SiteId = site.SiteId;
            adminRole.SiteGuid = site.SiteGuid;
            bool result = await userRepo.SaveRole(adminRole);
            adminRole.DisplayName = "Administrators";
            result = await userRepo.SaveRole(adminRole);

            SiteRole roleAdminRole = new SiteRole();
            roleAdminRole.DisplayName = "Role Admins";
            roleAdminRole.SiteId = site.SiteId;
            roleAdminRole.SiteGuid = site.SiteGuid;
            result = await userRepo.SaveRole(roleAdminRole);

            roleAdminRole.DisplayName = "Role Administrators";
            result = await userRepo.SaveRole(roleAdminRole);

            SiteRole contentAdminRole = new SiteRole();
            contentAdminRole.DisplayName = "Content Administrators";
            contentAdminRole.SiteId = site.SiteId;
            contentAdminRole.SiteGuid = site.SiteGuid;
            result = await userRepo.SaveRole(contentAdminRole);

            SiteRole authenticatedUserRole = new SiteRole();
            authenticatedUserRole.DisplayName = "Authenticated Users";
            authenticatedUserRole.SiteId = site.SiteId;
            authenticatedUserRole.SiteGuid = site.SiteGuid;
            result = await userRepo.SaveRole(authenticatedUserRole);

            //SiteRole newsletterAdminRole = new SiteRole();
            //newsletterAdminRole.DisplayName = "Newsletter Administrators";
            //newsletterAdminRole.SiteId = site.SiteId;
            //newsletterAdminRole.SiteGuid = site.SiteGuid;
            //userRepository.SaveRole(newsletterAdminRole);

            // if using related sites mode there is a problem if we already have user [email protected]
            // and we create another one in the child site with the same email and login so we need to make it different
            // we could just skip creating this user since in related sites mode all users come from the first site
            // but then if the config were changed to not related sites mode there would be no admin user
            // so in related sites mode we create one only as a backup in case settings are changed later
            int countOfSites = await siteRepo.GetCount();
            string siteDifferentiator = string.Empty;
            if (
                (countOfSites >= 1)
                && (multiTenantOptions.UseRelatedSitesMode)
                )
            {
                if (site.SiteId > 1)
                {
                    siteDifferentiator = site.SiteId.ToInvariantString();
                }
            }

            //mojoMembershipProvider membership = Membership.Provider as mojoMembershipProvider;
            //bool overridRelatedSiteMode = true;
            SiteUser adminUser = new SiteUser();
            adminUser.SiteId = site.SiteId;
            adminUser.SiteGuid = site.SiteGuid;
            adminUser.Email = "admin" + siteDifferentiator + "@admin.com";
            adminUser.DisplayName = "Admin";
            adminUser.UserName = "******" + siteDifferentiator;

            adminUser.EmailConfirmed = true;
            adminUser.ApprovedForLogin = true;
            adminUser.Password = "******";
            adminUser.PasswordFormat = 0;

            //if (membership != null)
            //{
            //    adminUser.Password = membership.EncodePassword(site, adminUser, "admin");
            //}

            adminUser.PasswordQuestion = "What is your user name?";
            adminUser.PasswordAnswer = "admin";

            result = await userRepo.Save(adminUser);

            //siteUserManager.AddPassword(adminUser.UserGuid.ToString(), "admin");

            //siteUserManager.Create(adminUser, "admin");
            //var result = siteUserManager.CreateAsync(adminUser, "admin");
            //if (result.Succeeded)
            //{
            //}
            result = await userRepo.AddUserToRole(
                adminRole.RoleId,
                adminRole.RoleGuid,
                adminUser.UserId,
                adminUser.UserGuid);

            return result;

        }