Ejemplo n.º 1
0
        // **************************************
        // RegisterUser
        // **************************************
        public static User RegisterUser(User user, Guid invitationCode)
        {
            using (var ctx = new SongSearchContext()) {
                var existing = ctx.GetUser(user);
                if (existing != null) {
                    return existing;
                } else {
                    var inv = ctx.Invitations.SingleOrDefault(i => i.InvitationId.Equals(invitationCode) && i.InvitationEmailAddress.Equals(user.UserName));
                    var pricingPlan = ctx.PricingPlans.SingleOrDefault(x => x.PricingPlanId == user.PricingPlanId);

                    if (inv == null) {
                        throw new ArgumentOutOfRangeException(String.Format("Invalid invitation {0}", inv.InvitationId), innerException: null);
                    } else {
                        // ----------------------------------
                        // CREATE USER
                        // ----------------------------------
                        var newUser = ctx.Create(user, inv, pricingPlan);
                        ctx.SaveChanges();

                        // ----------------------------------
                        // GET / CREATE PLAN SUBSCRIPTION
                        // ----------------------------------
                        if (!inv.IsPlanInvitation) {

                            // ----------------------------------
                            // GET / CREATE PLAN BALANCE
                            // ----------------------------------
                            var balance = ctx.SubscribeUserTo(newUser, pricingPlan);

                        } else {

                            newUser.PlanBalanceId = inv.InvitedByUser.PlanBalance.PlanBalanceId;
                            newUser.PlanUserId = inv.InvitedByUser.UserId;
                            ctx.AddToUserBalance(newUser);
                            ctx.AddToAdminBalance(newUser);

                        }

                        // ----------------------------------
                        // CATALOG ACCESS
                        // ----------------------------------

                        // Get parent users catalog where parent user is at least a plugger and assign to new user in client role
                        var catalogs = ctx.UserCatalogRoles.Where(x => x.UserId == inv.InvitedByUserId && x.RoleId <= (int)Roles.Admin);

                        catalogs.ForEach(c =>
                            newUser.UserCatalogRoles.Add(new UserCatalogRole() { CatalogId = c.CatalogId, RoleId = (int)Roles.Client })
                        );

                        inv.InvitationStatus = (int)InvitationStatusCodes.Registered;

                        ctx.SaveChanges();
                        inv = null;

                        return newUser;
                    }
                }

            }
        }
Ejemplo n.º 2
0
        // **************************************
        // UpdateProfile
        // **************************************
        public static bool ChangePassword(User user, string newPassword)
        {
            using (var ctx = new SongSearchContext()) {

                var dbuser = ctx.GetUser(user);
                if (dbuser == null) {
                    return false;
                }

                if (!String.IsNullOrEmpty(newPassword)) {
                    if (PasswordHashMatches(dbuser.Password, user.Password)) {
                        dbuser.Password = newPassword.PasswordHashString();
                    } else {
                        throw new ArgumentException("Passwords do not match");
                    }
                } else {
                    throw new ArgumentNullException("New password cannot be blank");
                }

                ctx.SaveChanges();
                dbuser = null;
                return true;
            }
        }
Ejemplo n.º 3
0
        // **************************************
        // UpdateUsersRole
        // **************************************
        public static void UpdateUsersRole(int userId, int roleId)
        {
            using (var ctx = new SongSearchContext()) {
                var user = ctx.GetUser(userId);

                if (user != null && ModelEnums.GetRoles().Contains(roleId)) {
                    user.RoleId = roleId;
                    if (roleId == (int)Roles.Admin) {
                        user.AddToAdminBalance();
                    } else if (user.RoleId == (int)Roles.Admin) {
                        user.RemoveFromAdminBalance();
                    }
                    user.RoleId = roleId;
                    ctx.SaveChanges();
                }
            }
        }
Ejemplo n.º 4
0
        // **************************************
        // UpdateUserRoleAllCatalogs
        // **************************************
        public static void UpdateAllCatalogs(int userId, int roleId)
        {
            using (var ctx = new SongSearchContext()) {

                var catalogs = ctx.Catalogs.AsQueryable();
                var user = ctx.GetUser(userId);

                if (!Account.User().IsSuperAdmin()) {

                    var parentUser = ctx.Users
                        .Include("UserCatalogRoles.Catalog")
                        .SingleOrDefault(u => u.UserId == user.ParentUserId);

                    // limit to catalogs with myUser admin access if not superadmin
                    catalogs = parentUser != null ? catalogs.LimitToAdministeredBy(parentUser) : catalogs;
                }

                foreach (var catalog in catalogs) {

                    var usrCatalog = user.UserCatalogRoles.SingleOrDefault(uc => uc.CatalogId == catalog.CatalogId);

                    if (usrCatalog == null && roleId > 0) {

                        // new catalog role
                        // check role against enum
                        roleId = ModelEnums.GetRoles().GetBestMatchForRole(roleId);

                        usrCatalog = new UserCatalogRole {
                            UserId = userId,
                            CatalogId = catalog.CatalogId,
                            RoleId = roleId
                        };
                        ctx.UserCatalogRoles.AddObject(usrCatalog);
                    } else {
                        if (roleId > 0) {
                            usrCatalog.RoleId = roleId;

                        } else {
                            // revoke access
                            ctx.UserCatalogRoles.DeleteObject(usrCatalog);
                        }
                    }
                }

                if (!user.IsInRole(Roles.Admin) && roleId == (int)Roles.Admin) {
                    // newly minted admin?
                    user.RoleId = roleId;
                    ctx.AddToAdminBalance(user);
                }
                ctx.SaveChanges();

            }
        }
Ejemplo n.º 5
0
        // **************************************
        // UpdateUsersRole
        // **************************************
        public static void ToggleSystemAdminAccess(int userId)
        {
            using (var ctx = new SongSearchContext()) {

                var user = ctx.GetUser(userId);

                var admin = (int)Roles.Admin;

                if (user != null && !user.IsSuperAdmin()) {

                    if (user.RoleId != admin) {
                        user.RoleId = admin;
                        ctx.AddToAdminBalance(user);
                    } else {
                        user.RoleId = (int)Roles.Client;
                        ctx.RemoveFromAdminBalance(user);
                    }

                    ctx.SaveChanges();

                }
            }
        }
Ejemplo n.º 6
0
 // **************************************
 // TakeOwnerShip
 // **************************************
 public static void TakeOwnerShip(int userId)
 {
     using (var ctx = new SongSearchContext()) {
         var user = ctx.GetUser(userId);
         if (user != null) {
             ctx.TakeOwnerShip(user);
             ctx.SaveChanges();
         }
         user = null;
     }
 }
Ejemplo n.º 7
0
        // **************************************
        // GetUserDetailWithCatalogsRoles
        // **************************************
        public static User GetUserDetailWithCatalogsRoles(int userId)
        {
            using (var ctx = new SongSearchContext()) {
                User user = ctx.GetUser(userId);
                user.ParentUser = user.ParentUserId.HasValue ? ctx.GetUser(user.ParentUserId.Value) : null;

                return user;
            }
        }
Ejemplo n.º 8
0
 // **************************************
 // UserExists
 // **************************************
 public static bool UserExists(string userName)
 {
     using (var ctx = new SongSearchContext()) {
         return ctx.GetUser(userName) != null ? true : false;
     }
 }
Ejemplo n.º 9
0
        // **************************************
        // UpdateProfile
        // **************************************
        public static IList<Contact> UpdateProfile(User user, IList<Contact> contacts)
        {
            using (var ctx = new SongSearchContext()) {

                var dbuser = ctx.GetUser(user);
                if (dbuser == null) {
                    throw new ArgumentException("User does not exist.");
                }

                dbuser.FirstName = user.FirstName;
                dbuser.LastName = user.LastName;
                dbuser.Signature = user.Signature;
                dbuser.AppendSignatureToTitle = user.AppendSignatureToTitle;
                dbuser.HasAllowedCommunication = user.HasAllowedCommunication;
                if (!contacts.Any(c => c.IsDefault)) {
                    contacts.First().IsDefault = true;
                }

                foreach (var contact in contacts) {
                    if (contact != null && (!String.IsNullOrWhiteSpace(contact.Phone1) || !String.IsNullOrWhiteSpace(contact.Email))) {
                        var dbContact = dbuser.Contacts.SingleOrDefault(c => c.ContactId == contact.ContactId) ??
                            new Contact() {
                                ContactTypeId = contact.ContactTypeId > 0 ? contact.ContactTypeId : (int)ContactTypes.Main,
                                //IsDefault = true,
                                UserId = dbuser.UserId,
                                CreatedOn = DateTime.Now
                            };

                        dbContact.IsDefault = contact.IsDefault;
                        dbContact.ContactName = contact.ContactName;
                        dbContact.CompanyName = contact.CompanyName;
                        dbContact.Address1 = contact.Address1;
                        dbContact.Address2 = contact.Address2;
                        dbContact.City = contact.City;
                        dbContact.StateRegion = contact.StateRegion;
                        dbContact.PostalCode = contact.PostalCode;
                        dbContact.Country = contact.Country;
                        dbContact.Phone1 = contact.Phone1;
                        dbContact.Phone2 = contact.Phone2;
                        dbContact.Fax = contact.Fax;
                        dbContact.Email = contact.Email;
                        dbContact.AdminEmail = contact.AdminEmail;

                        if (dbContact.ContactId == 0) {
                            ctx.Contacts.AddObject(dbContact);
                            dbuser.Contacts.Add(dbContact);
                        }
                    }
                }

                ctx.SaveChanges();

                return dbuser.Contacts.ToList();
            }
        }
Ejemplo n.º 10
0
        // **************************************
        // ResetPassword
        // **************************************
        public static bool ResetPassword(string userName, string resetCode, string newPassword)
        {
            using (var ctx = new SongSearchContext()) {

                var user = ctx.GetUser(userName);
                if (user == null) {
                    return false;
                }

                if (user.UserName.PasswordHashString().Equals(resetCode)) {
                    user.Password = newPassword.PasswordHashString();
                    ctx.SaveChanges();
                    user = null;
                    return true;
                } else {
                    user = null;
                    return false;
                }
            }
        }