public void ShareValidAccount()
        {
            UserManagement userManage = new UserManagement();
            AccountManagement accManage = new AccountManagement();

            // Owner            
            User Owner = userManage.CreateUser(new User
            {
                Email = "*****@*****.**",
                Password = "******",
                CreationDate = DateTime.Now,
                LastAccessDate = DateTime.Now
            });

            // Owner Account
            accManage.CreateAccount(new Account
            {
                AccountName = "ShareAccountTest",
                CreationDate = DateTime.Now,
                StartDate = DateTime.Now,
                FrequencyId = 3,
                AccountTypeId = 1,
                UserId = Owner.UserId
            });

            Account[] accounts = accManage.GetAccounts(Owner).ToArray();

            // Share user
            User shareUser = userManage.CreateUser(new User
            {
                Email = "*****@*****.**",
                Password = "******",
                CreationDate = DateTime.Now,
                LastAccessDate = DateTime.Now
            });

            // Share
            int accountID = accounts[0].AccountId;
            Share share = new Share();
            share.UserId = shareUser.UserId;
            share.AccountId = accounts[0].AccountId;
            share.PermissionLevelId = 2;

            accManage.ShareAccount(share);

            // Confirm
            accounts = accManage.GetAccounts(shareUser).ToArray();

            Account account = accounts.FirstOrDefault(x => x.AccountId == accountID);

            if (account == null)
                Assert.Fail("Shared account was not in Shared User collection.");

            if (!(account.IsShare))
                Assert.Fail("Shared account is not flagged as shared.");

            if (account.PermissionLevel < 1)
                Assert.Fail("Shared account permission level is invalid.");
        }
Beispiel #2
0
        public void ShareAccount(Share share)
        {
            try
            {
                using (CapitalContext context = new CapitalContext())
                {
                    // Check for shared user.
                    User user = context.Users.FirstOrDefault(x => x.UserId == share.UserId);
                    if (user == null)
                        throw new ModelException("That shared user does not exists.");

                    // Does this user already have this account?
                    Account account = context.Accounts.FirstOrDefault(x => x.UserId == share.UserId);
                    if (account != null)
                        throw new ModelException("Shared user is the owner of this account.");

                    // Is this already shared with the target?
                    Share existingShare = context.Share.FirstOrDefault(x => x.AccountId == share.AccountId && x.ShareId == user.UserId);
                    if (existingShare != null)
                        throw new ModelException("Account is already shared with this user.");

                    ValidationContext valContext = new ValidationContext(this, null, null);
                    var errors = share.Validate(valContext);
                    if (errors.Count() > 0)
                        throw new ModelException(errors);
                    else
                    {
                        context.Share.Add(share);
                        context.SaveChanges();
                    }
                }
            }
            catch (ModelException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                LogError(new object[] {share}, ex, System.Reflection.MethodBase.GetCurrentMethod().Name);
            }
        }
 public void ShareAccount(Share share)
 {
     try
     {
         AccountManagement accountManagement = new AccountManagement();
         accountManagement.ShareAccount(share);
     }
     catch (ModelException ex)
     {
         CapitalError error = new CapitalError(ex);
         throw new FaultException<CapitalError>(error, error.Message);
     }
     catch (Exception)
     {
         return;
     }
 }