Ejemplo n.º 1
0
        /// <summary>
        ///     Checks if user exist
        /// </summary>
        /// <param name="context">ITupapiContext</param>
        /// <param name="exception">Throw exception if found for email and name search</param>
        /// <param name="id">User Id</param>
        /// <param name="email">User Email</param>
        /// <param name="name">User Name</param>
        /// <returns>User</returns>
        public static User UserExist(ITupapiContext context, bool exception, string id = null, string email = null,
                                     string name = null)
        {
            User user = null;

            if (!string.IsNullOrWhiteSpace(id))
            {
                user = context.Users.SingleOrDefault(u => u.Id == id);
            }

            if (!string.IsNullOrWhiteSpace(email))
            {
                user = context.Users.SingleOrDefault(u => u.Email == email);
                if (user != null && exception)
                {
                    throw new ApiException(ApiResult.Validation, ErrorType.UserWithEmailExist, email);
                }
            }

            if (!string.IsNullOrWhiteSpace(name))
            {
                user = context.Users.SingleOrDefault(u => u.Name == name);
                if (user != null && exception)
                {
                    throw new ApiException(ApiResult.Validation, ErrorType.UserWithNameExist, name);
                }
            }


            return(user);
        }
Ejemplo n.º 2
0
        public static User CreateUser(ITupapiContext context, Provider provider, StandartAuthRequest request)
        {
            User   newUser      = null;
            string providerName = null;
            string providerId   = null;
            string accesstoken  = null;

            if (provider == Provider.Standart)
            {
                var salt = AuthHelper.GenerateSalt();
                newUser = new User
                {
                    Id    = SequentialGuid.NewGuid(),
                    Name  = request.Name,
                    Email = request.Email,
                    Salt  = salt,
                    SaltedAndHashedPassword = AuthHelper.Hash(request.Password, salt)
                };
                providerName = Const.Standart;
                providerId   = newUser.Id;
            }

            context.Users.Add(newUser);
            context.SaveChanges();
            CreateAccount(context, provider, providerName, newUser.Id, providerId);
            return(newUser);
        }
Ejemplo n.º 3
0
        public static Account AccountExist(ITupapiContext context, Provider provider, string userId)
        {
            var account = context.Accounts.SingleOrDefault(a => a.UserId == userId && a.Provider == provider);

            if (account == null)
            {
                throw new ApiException(ApiResult.NotFound, ErrorType.AccountNotFound, userId);
            }
            return(account);
        }
Ejemplo n.º 4
0
        public static User GetUser(ITupapiContext context, ClaimsPrincipal claimsPrincipal)
        {
            string userId = GetUserId(claimsPrincipal);
            var    user   = context.Users.AsNoTracking().SingleOrDefault(u => u.Id == userId);

            if (user == null)
            {
                throw new ApiException(ApiResult.Denied, ErrorType.UserNotFound, userId);
            }
            return(user);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Provide Standart Authentication
        /// </summary>
        /// <param name="context">ITupapiContext</param>
        /// <param name="req">Creds</param>
        /// <returns></returns>
        public static TestResult <LoginResult> Authenticate(ITupapiContext context, StandartAuthRequest req)
        {
            var             config     = new HttpConfiguration();
            LoginController controller = new LoginController(context)
            {
                Request = new HttpRequestMessage()
            };

            controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;

            HttpResponseMessage response = controller.Login(req);

            return(ParseLoginResponse(response));
        }
Ejemplo n.º 6
0
 public static void IsUserBlocked(ITupapiContext context, string userId = null, User user = null)
 {
     if (!string.IsNullOrWhiteSpace(userId))
     {
         user = UserExist(context, false, userId);
     }
     if (user == null)
     {
         throw new ApiException(ApiResult.Validation, ErrorType.UserNotFound, userId);
     }
     if (user.IsBlocked)
     {
         throw new ApiException(ApiResult.Denied, ErrorType.UserBlocked, user.Id);
     }
     // TODO: Причина блокировки
 }
Ejemplo n.º 7
0
 public static void CreateAccount(ITupapiContext context, Provider provider, string providerName, string userId,
                                  string providerId,
                                  string accesstoken = null)
 {
     try
     {
         Account newAccount = new Account
         {
             Id          = SequentialGuid.NewGuid(),
             AccountId   = providerName + ":" + userId,
             UserId      = userId,
             Provider    = provider,
             ProviderId  = providerId,
             AccessToken = accesstoken
         };
         context.Accounts.Add(newAccount);
         context.SaveChanges();
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.ToString());
         throw;
     }
 }
Ejemplo n.º 8
0
 public TestDbPopulator(ITupapiContext context)
 {
     _context = context;
 }
Ejemplo n.º 9
0
 public LoginController(ITupapiContext context)
 {
     _context = context;
     _config  = Mapping.Mapping.GetConfiguration();
     _mapper  = _config.CreateMapper();
 }
Ejemplo n.º 10
0
 public RegistrationController(ITupapiContext context)
 {
     _context = context;
 }
Ejemplo n.º 11
0
 public RegistrationController()
 {
     _context = new TupapiContext();
 }
Ejemplo n.º 12
0
 public UserApiController(ITupapiContext context)
 {
     _context = context;
     _config  = Mapping.Mapping.GetConfiguration();
 }
Ejemplo n.º 13
0
 public UserApiController()
 {
     _context = new TupapiContext();
     _config  = Mapping.Mapping.GetConfiguration();
 }
Ejemplo n.º 14
0
 public PostApiController()
 {
     _context = new TupapiContext();
     _config  = Mapping.Mapping.GetConfiguration();
     _mapper  = _config.CreateMapper();
 }
Ejemplo n.º 15
0
        public static DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry,
                                                              IDictionary <object, object> items, ITupapiContext context)
        {
            var result = new List <DbValidationError>();

            if (entityEntry.Entity is Account &&
                (entityEntry.State == EntityState.Added ||
                 entityEntry.State == EntityState.Modified))
            {
                var accountToCheck = (Account)entityEntry.Entity;

                //check for uniqueness of Account ID for User
                if (
                    context.Accounts.Any(
                        x => x.AccountId != accountToCheck.AccountId && x.UserId == accountToCheck.UserId))
                {
                    result.Add(new DbValidationError("AccountId",
                                                     $"The Account ID on Account with Provider: '{accountToCheck.Provider}' and Provider Id: '{accountToCheck.ProviderId}' for User Id: '{accountToCheck.UserId}' must be unique."));
                }
            }

            return(result.Count > 0 ? new DbEntityValidationResult(entityEntry, result) : null);
        }
Ejemplo n.º 16
0
 public BaseControllerTest()
 {
     TestContext     = new TestTupContext();
     TestDbPopulator = new TestDbPopulator(TestContext);
 }