// GET: User/Edit
        public ActionResult Edit(string id)
        {
            // Validate Id
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new PlaylistSharingDbContext())
            {
                // Get user from database
                var user = db.Users.FirstOrDefault(u => u.Id == id);

                // Check if user exists
                if (user == null)
                {
                    return(HttpNotFound());
                }

                // Create view model
                var viewModel = new EditUserViewModel();
                viewModel.User  = user;
                viewModel.Roles = GetUserRoles(user, db);

                // Pass the model to the view
                return(View(viewModel));
            }
        }
Пример #2
0
        public ActionResult AddSongsConfirmed(Playlist model)
        {
            if (ModelState.IsValid)
            {
                using (var database = new PlaylistSharingDbContext())
                {
                    // Get author id
                    var userId = database.Users
                                 .First(u => u.UserName == this.User.Identity.Name)
                                 .Id;


                    var playlist = new Playlist(userId, model.Title);


                    // Save article in DB
                    database.Playlists.Add(playlist);
                    database.SaveChanges();


                    return(RedirectToAction("UploadSongs", "File", new { playlistId = playlist.Id }));
                }
            }
            return(View(model));
        }
        private List <Role> GetUserRoles(ApplicationUser user, PlaylistSharingDbContext db)
        {
            // Create user manager
            var userManager = Request
                              .GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            // Get all application roles
            var roles = db.Roles
                        .Select(r => r.Name)
                        .OrderBy(r => r)
                        .ToList();

            // For each application role, check if the user has it
            var userRoles = new List <Role>();

            foreach (var roleName in roles)
            {
                var role = new Role()
                {
                    Name = roleName
                };

                if (userManager.IsInRole(user.Id, roleName))
                {
                    role.IsSelected = true;
                }

                userRoles.Add(role);
            }

            // Return a list with all roles
            return(userRoles);
        }
        public ActionResult UploadSongs(int?playlistId)
        {
            using (var db = new PlaylistSharingDbContext())
            {
                var playlist = db.Playlists.FirstOrDefault(p => p.Id == playlistId);


                return(View(playlist));
            }
        }
        // GET: User/List
        public ActionResult List()
        {
            using (var db = new PlaylistSharingDbContext())
            {
                var users = db.Users.ToList();

                var admins = GetAdminUserNames(users, db);
                ViewBag.Admins = admins;

                return(View(users));
            }
        }
        public JsonResult GetPlayerData(int?playlistId)
        {
            using (var db = new PlaylistSharingDbContext())
            {
                var song     = new AudioFile();
                var playlist = db.Playlists.FirstOrDefault(p => p.Id == playlistId);
                if (playlist != null)
                {
                    song = playlist.AudioFiles.FirstOrDefault();
                }
                var path = "http://" + Request.Url.Authority + "/Users/" + playlist.UserId + "/" + song.NameOnServer;

                return(Json(new { title = song.Title, url = path, duration = song.Duration }, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult DeleteConfirmed(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new PlaylistSharingDbContext())
            {
                // Get user from database
                var user = db.Users
                           .FirstOrDefault(u => u.Id.Equals(id));

                // Get user playlists from database
                var userPlaylists = db.Playlists
                                    .Where(u => u.User.Id.Equals(user.Id));

                // Delete user playlists
                foreach (var playlist in userPlaylists)
                {
                    db.Playlists.Remove(playlist);
                }

                // Get user ProfilePicturePaths from Database
                var userProfilePicturePaths = db.ProfilePicturePaths
                                              .Where(u => u.User.Id.Equals(user.Id));

                // Delete user profile pictures
                foreach (var userProfilePicturePath in userProfilePicturePaths)
                {
                    db.ProfilePicturePaths.Remove(userProfilePicturePath);
                }

                // Delete User folder with all the files on Server
                var fullPath             = Server.MapPath("~/Users/" + user.Id);
                var isUserDirectoryValid = Directory.Exists(fullPath);
                if (isUserDirectoryValid)
                {
                    Directory.Delete(fullPath, true);
                }

                // Delete user and save database
                db.Users.Remove(user);
                db.SaveChanges();

                return(RedirectToAction("List"));
            }
        }
        public ActionResult UploadSongsConfirmed(Playlist model, IEnumerable <HttpPostedFileBase> uploadSongs)
        {
            if (model == null || uploadSongs == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (ModelState.IsValid)
            {
                // Get server folder or create one for the current user
                var folder = Directory.CreateDirectory(Server.MapPath("~/Users"));
                folder.CreateSubdirectory(this.User.Identity.GetUserId());

                using (PlaylistSharingDbContext db = new PlaylistSharingDbContext())
                {
                    foreach (var file in uploadSongs)
                    {
                        if (file.ContentLength > 0)
                        {
                            // Upload to server folder /Users/{user.Id}/fileName
                            var fileName   = Path.GetFileName(file.FileName);
                            var serverPath = Path.Combine(Server.MapPath("~/Users"), this.User.Identity.GetUserId(), fileName);
                            file.SaveAs(serverPath);

                            TagLib.File fileDetails = TagLib.File.Create(serverPath);
                            // populate a song file

                            var song = new AudioFile(fileName, serverPath, fileDetails.Tag, fileDetails.Properties);

                            // Add songs to playlist
                            var playlist = db.Playlists.FirstOrDefault(p => p.Id == model.Id);
                            if (playlist != null)
                            {
                                playlist.AudioFiles.Add((song));
                            }
                        }
                    }

                    // Save to Database
                    db.SaveChanges();

                    return(RedirectToAction("List", "Playlist"));
                }
            }

            return(View());
        }
        private HashSet <string> GetAdminUserNames(List <ApplicationUser> users, PlaylistSharingDbContext context)
        {
            var userManager = new UserManager <ApplicationUser>(
                new UserStore <ApplicationUser>(context));

            var admins = new HashSet <string>();

            foreach (var user in users)
            {
                if (userManager.IsInRole(user.Id, "Admin"))
                {
                    admins.Add(user.UserName);
                }
            }

            return(admins);
        }
        private void SetUserRoles(EditUserViewModel viewModel, ApplicationUser user, PlaylistSharingDbContext context)
        {
            var userManager = HttpContext.GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            foreach (var role in viewModel.Roles)
            {
                if (role.IsSelected && !userManager.IsInRole(user.Id, role.Name))
                {
                    userManager.AddToRole(user.Id, role.Name);
                }
                else if (!role.IsSelected && userManager.IsInRole(user.Id, role.Name))
                {
                    userManager.RemoveFromRole(user.Id, role.Name);
                }
            }
        }
        public ActionResult DeleteSong(int?playlistId, int?songId)
        {
            if (playlistId == null || songId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new PlaylistSharingDbContext())
            {
                var playlist = db.Playlists.FirstOrDefault(p => p.Id == playlistId);

                // Check if playlist exists
                if (playlist == null)
                {
                    return(HttpNotFound());
                }

                var song = playlist.AudioFiles.FirstOrDefault(a => a.Id == songId);

                // Check if song exists
                if (song == null)
                {
                    return(HttpNotFound());
                }

                if (IsUserAuthorizedToEdit(playlist))
                {
                    var serverPath = Path.Combine(Server.MapPath("~/Users"), playlist.UserId, song.NameOnServer);
                    if (System.IO.File.Exists(serverPath))
                    {
                        System.IO.File.Delete(serverPath);
                    }

                    playlist.AudioFiles.Remove(song);
                }

                // Save
                db.SaveChanges();

                return(RedirectToAction("Details", "Playlist", new { id = playlistId }));
            }
        }
Пример #12
0
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            Playlist playlist;

            using (var database = new PlaylistSharingDbContext())
            {
                playlist = database.Playlists
                           .Where(a => a.Id == id)
                           .Include(a => a.User)
                           .Include(a => a.AudioFiles)
                           .FirstOrDefault();
            }

            return(View(playlist));
        }
Пример #13
0
        public ActionResult List()
        {
            List <Playlist> playlists;

            using (var database = new PlaylistSharingDbContext())
            {
                if (database.Playlists.Any())
                {
                    playlists = database.Playlists
                                .Include(u => u.User)
                                .Include(u => u.AudioFiles)
                                .ToList();
                }
                else
                {
                    playlists = new List <Playlist>();
                }
            }

            return(View(playlists));
        }
        public ActionResult Edit(string id, EditUserViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                using (var db = new PlaylistSharingDbContext())
                {
                    // Get user from database
                    var user = db.Users
                               .FirstOrDefault(a => a.Id == id);

                    // Check if user exists
                    if (user == null)
                    {
                        return(HttpNotFound());
                    }

                    // If password field is not empty, change password
                    if (!string.IsNullOrEmpty(viewModel.Password))
                    {
                        var hasher       = new PasswordHasher();
                        var passwordHash = hasher.HashPassword(viewModel.Password);
                        user.PasswordHash = passwordHash;
                    }
                    // Set user properties
                    user.Email    = viewModel.User.Email;
                    user.FullName = viewModel.User.FullName;
                    user.UserName = viewModel.User.Email;
                    this.SetUserRoles(viewModel, user, db);

                    // Save changes
                    db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("List"));
                }
            }

            return(View(viewModel));
        }
        // GET: User/Delete
        public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new PlaylistSharingDbContext())
            {
                // Get user from database
                var user = db.Users
                           .FirstOrDefault(u => u.Id.Equals(id));

                // Check if user exists
                if (user == null)
                {
                    return(HttpNotFound());
                }

                return(View(user));
            }
        }
Пример #16
0
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var db = new PlaylistSharingDbContext())
            {
                var playlist = db.Playlists.FirstOrDefault(p => p.Id == id);

                // Check if playlist exists
                if (playlist == null)
                {
                    return(HttpNotFound());
                }


                if (IsUserAuthorizedToEdit(playlist))
                {
                    foreach (var song in playlist.AudioFiles)
                    {
                        var serverPath = Path.Combine(Server.MapPath("~/Users"), playlist.UserId, song.NameOnServer);
                        if (System.IO.File.Exists(serverPath))
                        {
                            System.IO.File.Delete(serverPath);
                        }
                    }
                    playlist.AudioFiles.Clear();
                    db.Playlists.Remove(playlist);
                }

                // Save
                db.SaveChanges();

                return(RedirectToAction("List", "Playlist"));
            }
        }