// **************************************
        // SaveCatalog
        // **************************************
        private CatalogUploadState SaveCatalog(CatalogUploadState state)
        {
            System.Diagnostics.Debug.Write("Step5");

            var user = Account.User();
            // Save/create Catalog
            if (user.IsAtLeastInRole(Roles.Admin) && !user.MyBalances().NumberOfSongs.IsAtTheLimit) {

                using (var ctx = new SongSearchContext()) {

                    var catalog = ctx.Catalogs.SingleOrDefault(c => c.CatalogName.ToUpper() == state.CatalogName) ??
                        new Catalog() {
                            CatalogName = state.CatalogName,
                            CreatedByUserId = user.UserId,
                            CreatedOn = DateTime.Now
                        };

                    if (catalog.CatalogId == 0) {

                        ctx.Catalogs.AddObject(catalog);
                        //DataSession.CommitChanges();

                        //Make current user an admin
                        var userCatalog = new UserCatalogRole() {
                            UserId = user.UserId,
                            CatalogId = catalog.CatalogId,
                            RoleId = (int)Roles.Admin
                        };
                        ctx.UserCatalogRoles.AddObject(userCatalog);

                        //Make parent user an admin
                        if (user.ParentUserId.HasValue) {
                            var parentUserCatalog =
                                //DataSession.Single<UserCatalogRole>(
                                //    x => x.UserId == user.ParentUserId.Value &&
                                //        x.CatalogId == catalog.CatalogId &&
                                //        x.RoleId == (int)Roles.Admin
                                //        ) ??
                                new UserCatalogRole() {
                                    UserId = user.ParentUserId.Value,
                                    CatalogId = catalog.CatalogId,
                                    RoleId = (int)Roles.Admin
                                };
                            ctx.UserCatalogRoles.AddObject(parentUserCatalog);
                        }

                        //Make plan user an admin
                        if (!user.IsPlanOwner && user.PlanUserId != user.ParentUserId.GetValueOrDefault()) {
                            var planUserCatalog =
                                //DataSession.Single<UserCatalogRole>(
                                //    x => x.UserId == user.PlanUserId &&
                                //        x.CatalogId == catalog.CatalogId &&
                                //        x.RoleId == (int)Roles.Admin
                                //        ) ??
                                new UserCatalogRole() {
                                    UserId = user.PlanUserId,
                                    CatalogId = catalog.CatalogId,
                                    RoleId = (int)Roles.Admin
                                };
                            ctx.UserCatalogRoles.AddObject(planUserCatalog);
                        }

                        // defer?
                        ctx.SaveChanges();
                    }

                    state.CatalogId = catalog.CatalogId;
                    state.CatalogName = catalog.CatalogName.ToUpper();

                    // Save Content
                    var content = App.IsLicensedVersion ?
                        state.Content.Take(user.MyBalances().NumberOfSongs.IsGoodFor(state.Content.Count())).ToList() :
                        state.Content;

                    foreach (var itm in content) {

                        itm.CatalogId = state.CatalogId;
                        itm.CreatedByUserId = user.UserId;
                        itm.CreatedOn = DateTime.Now;
                        itm.LastUpdatedByUserId = user.UserId;
                        itm.LastUpdatedOn = DateTime.Now;
                        //itm.IsMediaOnRemoteServer = false;

                        itm.Title = itm.Title.AsEmptyIfNull();//.CamelCase();//.ToUpper();
                        itm.Artist = itm.Artist.AsEmptyIfNull();//.CamelCase();//.ToUpper();
                        itm.RecordLabel = itm.RecordLabel.AsEmptyIfNull();//.CamelCase();//.ToUpper();
                        itm.ReleaseYear = itm.ReleaseYear.GetValueOrDefault().AsNullIfZero();
                        itm.Notes = itm.Notes;

                        var full = itm.UploadFiles.SingleOrDefault(f => f.FileMediaVersion == MediaVersion.Full);
                        foreach (var version in ModelEnums.MediaVersions()) {

                            var upl = itm.UploadFiles.SingleOrDefault(f => f.FileMediaVersion == version);
                            if (upl != null) {

                                var file = new FileInfo(upl.FilePath);
                                var id3 = ID3Writer.NormalizeTag(upl.FilePath, itm);

                                var media = new ContentMedia() {

                                    MediaVersion = (int)version,
                                    MediaType = "mp3",
                                    MediaSize = file.Length,
                                    MediaLength = id3.MediaLength,
                                    MediaDate = file.GetMediaDate(),
                                    MediaBitRate = id3.GetBitRate(file.Length)
                                };

                                media.IsRemote = false;

                                itm.ContentMedia.Add(media);
                            }

                        }
                        ctx.Contents.AddObject(itm);
                        ctx.AddToSongsBalance(user);
                        ctx.SaveChanges();

                        if (itm.ContentId > 0) {
                            foreach (var file in itm.UploadFiles) {
                                MediaService.SaveContentMedia(file.FilePath, itm.Media(file.FileMediaVersion));
                            }
                        }

                    }

                }
            }

            SessionService.Session().InitializeSession(true);
            CacheService.InitializeApp(true);

            return state;
        }
Esempio n. 2
0
 /// <summary>
 /// Create a new UserCatalogRole object.
 /// </summary>
 /// <param name="userId">Initial value of the UserId property.</param>
 /// <param name="catalogId">Initial value of the CatalogId property.</param>
 /// <param name="roleId">Initial value of the RoleId property.</param>
 public static UserCatalogRole CreateUserCatalogRole(global::System.Int32 userId, global::System.Int32 catalogId, global::System.Int32 roleId)
 {
     UserCatalogRole userCatalogRole = new UserCatalogRole();
     userCatalogRole.UserId = userId;
     userCatalogRole.CatalogId = catalogId;
     userCatalogRole.RoleId = roleId;
     return userCatalogRole;
 }
Esempio n. 3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the UserCatalogRoles EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUserCatalogRoles(UserCatalogRole userCatalogRole)
 {
     base.AddObject("UserCatalogRoles", userCatalogRole);
 }
        // **************************************
        // SetCatalogRole
        // **************************************
        private static void SetCatalogRole(this SongSearchContext ctx, int catalogId, int roleId, IList<User> users)
        {
            foreach (var user in users) {

                var usrCatalog = ctx.UserCatalogRoles.SingleOrDefault(uc => uc.CatalogId == catalogId && uc.UserId == user.UserId);

                roleId = ModelEnums.GetRoles().GetBestMatchForRole(roleId);

                if (usrCatalog == null && roleId > 0) { // does not currently have access
                    usrCatalog = new UserCatalogRole {
                        UserId = user.UserId,
                        CatalogId = catalogId,
                        RoleId = roleId
                    };
                    ctx.UserCatalogRoles.AddObject(usrCatalog);
                } else {
                    if (roleId > 0) { //does not currently have the desired access level
                        usrCatalog.RoleId = roleId;
                        if (!user.IsInRole(Roles.Admin) && roleId == (int)Roles.Admin) {
                            // newly minted admin?
                            user.RoleId = roleId;
                            ctx.AddToAdminBalance(user);
                        }
                    } else {
                        ctx.UserCatalogRoles.DeleteObject(usrCatalog);
                    }
                }

                if (user.ChildUsers.Count > 0) {

                    ctx.SetCatalogRole(catalogId, roleId, user.ChildUsers);

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

            }
        }