internal BrightstarAccountsRepository(string superUserAccountId, string superUserAccountKey, IAccountsCache cache)
 {
     Task.Factory.StartNew(() =>
                               {
                                   while (true)
                                   {
                                       try
                                       {
                                           try
                                           {
                                               BrightstarCluster.Instance.GetLastModifiedDate(StoreName);
                                               return;
                                           }
                                           catch (StoreNotFoundException)
                                           {
                                               Trace.TraceInformation("Attempting to create Accounts Repository");
                                               BrightstarCluster.Instance.CreateStore(StoreName);
                                           }
                                       }
                                       catch (EndpointNotFoundException)
                                       {
                                           Thread.Sleep(3000);
                                       }
                                       catch(Exception)
                                       {
                                           return;
                                       }
                                   }
                               });
     _superUserAccount = new AccountDetails{AccountId = superUserAccountId, PrimaryKey = superUserAccountKey, SecondaryKey = superUserAccountKey};
     _cache = cache;
 }
 public void CacheAccountDetails(AccountDetails accountDetails, string accountOrUserId)
 {
     _cache.Add("AccountDetails:" + accountOrUserId,
                accountDetails,
                null,
                DateTime.Now.AddMinutes(15.0),
                Cache.NoSlidingExpiration,
                CacheItemPriority.Normal,
                null);
 }
 private static bool ValidateSignature(HttpRequestBase request, AccountDetails accessAccount, SignatureType sigType, string signature)
 {
     if (accessAccount != null)
     {
         if (signature.Equals(RestClientHelper.GenerateSignature(request, sigType,
                                                                 accessAccount.PrimaryKey)) ||
             signature.Equals(RestClientHelper.GenerateSignature(request, sigType,
                                                                 accessAccount.SecondaryKey)))
         {
             return true;
         }
     }
     return false;
 }
 public AccountDetails UpdateAccount(string accountId, bool isAdmin)
 {
     var context = GetAccountsContext();
     var account = context.Accounts.FirstOrDefault(x => x.Id.Equals(accountId));
     if (account != null)
     {
         account.IsAdmin = isAdmin;
         context.SaveChanges();
         var accountDetails = new AccountDetails(account);
         _cache.CacheAccountDetails(accountDetails, accountId);
         return accountDetails;
     }
     throw new AccountsRepositoryException(AccountsRepositoryException.UserAccountNotFound, "No account found with ID " + accountId);
 }
 public AccountDetails GetAccountDetails(string accountId)
 {
     var accountDetails = _cache.LookupAccountDetails(accountId);
     if (accountDetails == null)
     {
         var context = GetAccountsContext();
         var account = context.Accounts.FirstOrDefault(a => a.Id.Equals(accountId));
         if (account == null) return null;
         accountDetails = new AccountDetails(account);
         _cache.CacheAccountDetails(accountDetails, accountId);
     }
     return accountDetails;
 }
 /// <summary>
 /// Retrieves the information about a user's account
 /// </summary>
 /// <param name="userToken">The user token for the user</param>
 /// <returns>A structure giving the account ID and the subscriptions attached to the account</returns>
 public AccountDetails GetAccountDetailsForUser(string userToken)
 {
     var accountDetails = _cache.LookupAccountDetails(userToken);
     if (accountDetails == null)
     {
         var context = GetAccountsContext();
         var account = context.Accounts.FirstOrDefault(a => a.UserTokens.Any(ut => ut.Id.Equals(userToken)));
         if (account == null) return null;
         accountDetails = new AccountDetails(account);
         _cache.CacheAccountDetails(accountDetails, userToken);
     }
     return accountDetails;
 }
 public AccountAndSubscription(AccountDetails account, SubscriptionDetails subscription, string apiEndpoint)
 {
     Account = account;
     Subscription = subscription;
     ApiEndpoint = apiEndpoint;
 }
        private bool ValidateAccessPrivileges(HttpRequestBase request, AccountDetails accessAccount)
        {
            if (_requiredAccessLevel == StoreAccessLevel.Superuser)
            {
                // Already validated signature against super user credentials
                return true;
            }

            var routeData = request.RequestContext.RouteData;
            object tmp;
            if (routeData.Values != null && routeData.Values.TryGetValue("subscription", out tmp))
            {
                var subscriptionId = tmp.ToString();
                if (_requiredAccessLevel == StoreAccessLevel.Owner)
                {
                    // Owner access is checked against the subscription - the access account must own the subscription being accessed.
                    return accessAccount.Subscriptions.Any(s => s.Id.Equals(subscriptionId));
                }
                if (routeData.Values.TryGetValue("storeId", out tmp))
                {
                    var storeId = tmp.ToString();
                    var accRepo = AccountsRepositoryFactory.GetAccountsRepository();
                    var accessKey = accRepo.GetAccountAccessKey(accessAccount.AccountId, storeId);
                    if (accessKey != null && (accessKey.Access & _requiredAccessLevel) > 0)
                    {
                        routeData.Values["_accessLevel"] = accessKey.Access;
                        return true;
                    }
                } else
                {
                    // No store id - this should only be for listing stores
                    return true;
                }
            }
            return false;
        }
 public void CacheAccountDetails(AccountDetails accountDetails, string accountOrUserId)
 {
     return;
 }