Esempio n. 1
0
        public async Task <UserAccount> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
        {
            UserAccount user;

            using (IDbConnection db = new SqlConnection(_connectionString.GetConnectionString()))
            {
                const string sqlQuery = "SELECT * FROM [UriReduction].[User] where UserName=@userName";
                user = await db.QuerySingleOrDefaultAsync <UserAccount>(sqlQuery, new { userName = normalizedUserName });

                if (user == null)
                {
                    return(null);
                }
                if (user.Id == null)
                {
                    return(user);
                }
                var userRoles = _userRoleRepository.GetAllRoleIdByUserId(user.Id.Value);
                foreach (var userRole in userRoles)
                {
                    user.UserRoles.Add(
                        (await _roleStore.FindByIdAsync(userRole.ToString(), new CancellationToken())).Name);
                }
            }
            return(user);
        }
Esempio n. 2
0
        public async Task <IList <string> > GetRolesAsync(TUser user, CancellationToken cancellationToken)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            cancellationToken.ThrowIfCancellationRequested();

            var userDb = await ByIdAsync(user.Id, cancellationToken).ConfigureAwait(true);

            if (userDb == null)
            {
                return(new List <string>());
            }

            var roles = new List <string>();

            foreach (var item in userDb.Roles)
            {
                var dbRole = await _roleStore.FindByIdAsync(item, cancellationToken).ConfigureAwait(true);

                if (dbRole != null)
                {
                    roles.Add(dbRole.Name);
                }
            }
            return(roles);
        }
Esempio n. 3
0
        public async Task RoleStore_create_find_by_id()
        {
            var role = _roleTestFactory.CreateRole();
            await _roleStore.CreateAsync(role, default(CancellationToken));

            await _roleStore.SetNormalizedRoleNameAsync(role, role.Name, default(CancellationToken));

            var fetchedRole = await _roleStore.FindByIdAsync(role.Id, default(CancellationToken));

            role.Name.ShouldMatch(fetchedRole.Name);
        }
Esempio n. 4
0
        private async Task SetRoleValueAsync(TUser user, string roleName, bool newValue, CancellationToken cancellationToken)
        {
            user.CheckNotNull(nameof(user));
            roleName.CheckNotNullOrEmpty(nameof(roleName));

            // Validate role name.
            var role = await _roleStore.FindByIdAsync(roleName, cancellationToken).ConfigureAwait(false);

            if (role == null)
            {
                throw new ArgumentException("roleName value '{0}' is not valid role name.".FormatInvariant(roleName));
            }

            // Add/remove from Ontraport.
            var contact = new TContact();

            contact.SetRole(roleName, newValue);
            var result = await _ontraportContacts.UpdateAsync(user.Id, contact.GetChanges(), cancellationToken).ConfigureAwait(false);

            if (result?.Id == null)
            {
                throw new InvalidOperationException("Could not update contact with ID {0}".FormatInvariant(user.Id));
            }

            // Add/remove role in memory.
            lock (user.Roles)
            {
                if (newValue && !user.Roles.Contains(role.Id))
                {
                    user.Roles.Add(role.Id);
                }
                else if (!newValue && user.Roles.Contains(role.Id))
                {
                    user.Roles.Remove(role.Id);
                }
            }
        }
Esempio n. 5
0
        public async Task <IList <string> > GetRolesAsync(TUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var userDb = await ById(user.Id);

            if (userDb == null)
            {
                return(new List <string>());
            }

            var roles = new List <string>();

            foreach (var item in userDb.Roles)
            {
                var dbRole = await _roleStore.FindByIdAsync(item, cancellationToken);

                if (dbRole != null)
                {
                    roles.Add(dbRole.Name);
                }
            }
            return(roles);
        }
Esempio n. 6
0
 public async Task <ApplicationRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
 {
     return(await roleStore.FindByIdAsync(roleId, cancellationToken));
 }
Esempio n. 7
0
 public async Task <IdentityRole> FindRoleByIdAsync(string roleId)
 {
     return(await _roleStore.FindByIdAsync(roleId, CancellationToken.None));
 }