// **************************************
        // AddToAdminBalance
        // **************************************
        public static void AddToAdminBalance(this User user)
        {
            using (var ctx = new SongSearchContext()) {

                ctx.AddToAdminBalance(user);
                ctx.SaveChanges();

            }
        }
        // **************************************
        // UpdateUserCatalogRole
        // **************************************
        public static void UpdateUserCatalogRole(int userId, int catalogId, int roleId)
        {
            using (var ctx = new SongSearchContext()) {

                var user = ctx.GetUserDetail(userId);

                if (user != null && catalogId > 0) {

                    var usrCatalogRole = user.UserCatalogRoles.Where(c => c.CatalogId == catalogId).SingleOrDefault();

                    if (usrCatalogRole == null) {
                        if (roleId > 0) {
                            // new catalog role

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

                            usrCatalogRole =
                                new UserCatalogRole {
                                    UserId = userId,
                                    CatalogId = catalogId,
                                    RoleId = roleId
                                };

                            ctx.UserCatalogRoles.AddObject(usrCatalogRole);
                        }
                    } else {

                        if (roleId > 0) {
                            // update role
                            usrCatalogRole.RoleId = roleId;

                        } else {

                            //also remove these catalogs from any child users, really?
                            var childUsers = user.MyUserHierarchy(withCatalogRoles: true);
                            foreach (var child in childUsers) {
                                var cat = ctx.UserCatalogRoles.SingleOrDefault(x => x.CatalogId == catalogId && x.UserId == child.UserId);
                                if (cat != null) { ctx.UserCatalogRoles.DeleteObject(cat); }
                            }

                            // revoke parent access
                            ctx.UserCatalogRoles.DeleteObject(usrCatalogRole);

                        }
                    }

                    // check if it's an admin role; if so, elevate the system role to Admin if user is not already there
                    if (roleId == (int)Roles.Admin && user.RoleId > roleId) {
                        user.RoleId = roleId;
                        ctx.AddToAdminBalance(user);
                    }
                    ctx.SaveChanges();

                }
            }
        }
        // **************************************
        // 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();

            }
        }
        // **************************************
        // 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();

                }
            }
        }
Beispiel #5
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;
                    }
                }

            }
        }