<commit_before>﻿/*
 * Copyright 2014 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Thinktecture.IdentityManager;

namespace Thinktecture.IdentityManager.AspNetIdentity
{
    public class AspNetIdentityManagerService<TUser, TUserKey, TRole, TRoleKey> : IIdentityManagerService
        where TUser : class, IUser<TUserKey>, new()
        where TUserKey : IEquatable<TUserKey>
        where TRole : class, IRole<TRoleKey>, new()
        where TRoleKey : IEquatable<TRoleKey>
    {
        protected Microsoft.AspNet.Identity.UserManager<TUser, TUserKey> userManager;
        protected Func<string, TUserKey> ConvertUserSubjectToKey;

        protected Microsoft.AspNet.Identity.RoleManager<TRole, TRoleKey> roleManager;
        protected Func<string, TRoleKey> ConvertRoleSubjectToKey;

        Func<Task<IdentityManagerMetadata>> metadataFunc;

        AspNetIdentityManagerService(UserManager<TUser, TUserKey> userManager, RoleManager<TRole, TRoleKey> roleManager)
        {
            if (userManager == null) throw new ArgumentNullException("userManager");
            if (roleManager == null) throw new ArgumentNullException("roleManager");

            if (!userManager.SupportsQueryableUsers)
            {
                throw new InvalidOperationException("UserManager must support queryable users.");
            }

            this.userManager = userManager;
            this.roleManager = roleManager;

            if (userManager.UserTokenProvider == null)
            {
                userManager.UserTokenProvider = new TokenProvider<TUser, TUserKey>();
            }

            var keyType = typeof(TUserKey);
            if (keyType == typeof(string)) ConvertUserSubjectToKey = subject => (TUserKey)ParseString(subject);
            else if (keyType == typeof(int)) ConvertUserSubjectToKey = subject => (TUserKey)ParseInt(subject);
            else if (keyType == typeof(long)) ConvertUserSubjectToKey = subject => (TUserKey)ParseLong(subject);
            else if (keyType == typeof(Guid)) ConvertUserSubjectToKey = subject => (TUserKey)ParseGuid(subject);
            else
            {
                throw new InvalidOperationException("User Key type not supported");
            }

            keyType = typeof(TRoleKey);
            if (keyType == typeof(string)) ConvertRoleSubjectToKey = subject => (TRoleKey)ParseString(subject);
            else if (keyType == typeof(int)) ConvertRoleSubjectToKey = subject => (TRoleKey)ParseInt(subject);
            else if (keyType == typeof(long)) ConvertRoleSubjectToKey = subject => (TRoleKey)ParseLong(subject);
            else if (keyType == typeof(Guid)) ConvertRoleSubjectToKey = subject => (TRoleKey)ParseGuid(subject);
            else
            {
                throw new InvalidOperationException("Role Key type not supported");
            }
        }

        public AspNetIdentityManagerService(
            UserManager<TUser, TUserKey> userManager,
            RoleManager<TRole, TRoleKey> roleManager,
            bool includeAccountProperties = true)
            : this(userManager, roleManager)
        {
            this.metadataFunc = () => Task.FromResult(GetStandardMetadata(includeAccountProperties));
        }

        public AspNetIdentityManagerService(
           UserManager<TUser, TUserKey> userManager,
            RoleManager<TRole, TRoleKey> roleManager,
           IdentityManagerMetadata metadata)
            : this(userManager, roleManager, () => Task.FromResult(metadata))
        {
        }

        public AspNetIdentityManagerService(
           UserManager<TUser, TUserKey> userManager,
            RoleManager<TRole, TRoleKey> roleManager,
           Func<Task<IdentityManagerMetadata>> metadataFunc)
            : this(userManager, roleManager)
        {
            this.metadataFunc = metadataFunc;
        }

        object ParseString(string sub)
        {
            return sub;
        }
        object ParseInt(string sub)
        {
            int key;
            if (!Int32.TryParse(sub, out key)) return 0;
            return key;
        }
        object ParseLong(string sub)
        {
            long key;
            if (!Int64.TryParse(sub, out key)) return 0;
            return key;
        }
        object ParseGuid(string sub)
        {<commit_msg>Optional parameters to specify how to parse TUserKey and TRoleKey. Also add UInt32 parsing when guessing from type.<commit_after>﻿/*
 * Copyright 2014 <NAME>, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Thinktecture.IdentityManager;

namespace Thinktecture.IdentityManager.AspNetIdentity
{
    public class AspNetIdentityManagerService<TUser, TUserKey, TRole, TRoleKey> : IIdentityManagerService
        where TUser : class, IUser<TUserKey>, new()
        where TUserKey : IEquatable<TUserKey>
        where TRole : class, IRole<TRoleKey>, new()
        where TRoleKey : IEquatable<TRoleKey>
    {
        protected Microsoft.AspNet.Identity.UserManager<TUser, TUserKey> userManager;
        protected Func<string, TUserKey> ConvertUserSubjectToKey;

        protected Microsoft.AspNet.Identity.RoleManager<TRole, TRoleKey> roleManager;
        protected Func<string, TRoleKey> ConvertRoleSubjectToKey;

        Func<Task<IdentityManagerMetadata>> metadataFunc;

        AspNetIdentityManagerService(UserManager<TUser, TUserKey> userManager, RoleManager<TRole, TRoleKey> roleManager, Func<string, TUserKey> parseUserSubject = null, Func<string, TRoleKey> parseRoleSubject = null)
        {
            if (userManager == null) throw new ArgumentNullException("userManager");
            if (roleManager == null) throw new ArgumentNullException("roleManager");

            if (!userManager.SupportsQueryableUsers)
            {
                throw new InvalidOperationException("UserManager must support queryable users.");
            }

            this.userManager = userManager;
            this.roleManager = roleManager;

            if (userManager.UserTokenProvider == null)
            {
                userManager.UserTokenProvider = new TokenProvider<TUser, TUserKey>();
            }

            if (parseUserSubject != null)
            {
                ConvertUserSubjectToKey = parseUserSubject;
            }
            else
            {
                var keyType = typeof (TUserKey);
                if (keyType == typeof (string)) ConvertUserSubjectToKey = subject => (TUserKey) ParseString(subject);
                else if (keyType == typeof (int)) ConvertUserSubjectToKey = subject => (TUserKey) ParseInt(subject);
                else if (keyType == typeof (uint)) ConvertUserSubjectToKey = subject => (TUserKey) ParseUInt32(subject);
                else if (keyType == typeof (long)) ConvertUserSubjectToKey = subject => (TUserKey) ParseLong(subject);
                else if (keyType == typeof (Guid)) ConvertUserSubjectToKey = subject => (TUserKey) ParseGuid(subject);
                else
                {
                    throw new InvalidOperationException("User Key type not supported");
                }
            }

            if (parseRoleSubject != null)
            {
                ConvertRoleSubjectToKey = parseRoleSubject;
            }
            else
            {
                var keyType = typeof (TRoleKey);
                if (keyType == typeof (string)) ConvertRoleSubjectToKey = subject => (TRoleKey) ParseString(subject);
                else if (keyType == typeof (int)) ConvertRoleSubjectToKey = subject => (TRoleKey) ParseInt(subject);
                else if (keyType == typeof (uint)) ConvertUserSubjectToKey = subject => (TUserKey) ParseUInt32(subject);
                else if (keyType == typeof (long)) ConvertRoleSubjectToKey = subject => (TRoleKey) ParseLong(subject);
                else if (keyType == typeof (Guid)) ConvertRoleSubjectToKey = subject => (TRoleKey) ParseGuid(subject);
                else
                {
                    throw new InvalidOperationException("Role Key type not supported");
                }
            }
        }

        public AspNetIdentityManagerService(
            UserManager<TUser, TUserKey> userManager,
            RoleManager<TRole, TRoleKey> roleManager,
            bool includeAccountProperties = true,
            Func<string, TUserKey> parseUserSubject = null, Func<string, TRoleKey> parseRoleSubject = null)
            : this(userManager, roleManager, parseUserSubject, parseRoleSubject)
        {
            this.metadataFunc = () => Task.FromResult(GetStandardMetadata(includeAccountProperties));
        }

        public AspNetIdentityManagerService(
           UserManager<TUser, TUserKey> userManager,
           RoleManager<TRole, TRoleKey> roleManager,
           IdentityManagerMetadata metadata,
           Func<string, TUserKey> parseUserSubject = null, Func<string, TRoleKey> parseRoleSubject = null)
            : this(userManager, roleManager, () => Task.FromResult(metadata), parseUserSubject, parseRoleSubject)
        {
        }

        public AspNetIdentityManagerService(
           UserManager<TUser, TUserKey> userManager,
           RoleManager<TRole, TRoleKey> roleManager,
           Func<Task<IdentityManagerMetadata>> metadataFunc,
           Func<string, TUserKey> parseUserSubject = null, Func<string, TRoleKey> parseRoleSubject = null)
            : this(userManager, roleManager, parseUserSubject, parseRoleSubject)
        {
            this.metadataFunc = metadataFunc;
        }

        object ParseString(string sub)
        {
            return sub;
        }
        object ParseInt(string sub)
        {
            int key;
            if (!Int32.TryParse(sub, out key)) return 0;
            return key;
        }
        object ParseUInt32(string sub)
        {
            uint key;
            if (!UInt32.TryParse(sub, out key)) return 0;
            return key;
        }
        object ParseLong(string sub)
        {
            long key;
            if (!Int64.TryParse(sub, out key)) return 0;
            return key;
        }
        object ParseGuid(string sub)
        {
{'fraction_non_alphanumeric': 0.08498829242910415, 'fraction_numerical': 0.0033821871476888386, 'mean_word_length': 2.5147820786345627, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4735636', 'n_tokens_mistral': 2956, 'n_tokens_neox': 2874, 'n_words': 779}