예제 #1
0
        // ShareAlbum <albumId> <username> <permission>
        public string Execute(string[] data)
        {
            int      albumId  = int.Parse(data[0]);
            AlbumDto albumDTO = albumService.ById <AlbumDto>(albumId);

            if (albumDTO == null)
            {
                throw new ObjectNotFoundException(typeof(Album).Name, data[0]);
            }
            string       username = data[1];
            UserRolesDto userDTO  = userService.ByUsername <UserRolesDto>(username);

            if (userDTO == null || userDTO.IsDeleted == true)
            {
                throw new ObjectNotFoundException(typeof(User).Name, username);
            }
            if (!Enum.TryParse(data[2], true, out Role role))
            {
                throw new InvalidPermissionException();
            }
            string       permission         = role.ToString();
            AlbumRoleDto alreadySharedAlbum = userDTO.Permissions
                                              .FirstOrDefault(p => p.AlbumName == albumDTO.Name);

            if (alreadySharedAlbum != null && alreadySharedAlbum.Permission == permission)
            {
                throw new PermissionAlreadyGrantedException(username, permission, albumDTO.Name);
            }
            AlbumRole albumRole = albumRoleService.PublishAlbumRole(albumId, userDTO.Id, permission);

            return(String.Format(SuccessMessage, username, albumDTO.Name, permission));
        }
예제 #2
0
        // ShareAlbum <albumId> <username> <permission>
        // For example:
        // ShareAlbum 4 dragon321 Owner
        // ShareAlbum 4 dragon11 Viewer
        public string Execute(string[] data)
        {
            int    albumId    = int.Parse(data[0]);
            string username   = data[1];
            string permission = data[2];

            if (albumRoleService.GetOwnerId(albumId) != Session.CurrentUser.Id)
            {
                return(OperationNotAllowed);
            }

            if (!userService.Exists(username))
            {
                return(String.Format(UserNotFound, username));
            }

            if (!albumService.Exists(albumId))
            {
                return(String.Format(AlbumNotFound, albumId));
            }

            var user  = userService.ByUsername <User>(username);
            var album = albumService.ById <Album>(albumId);

            try
            {
                albumRoleService.PublishAlbumRole(albumId, user.Id, permission);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            return(String.Format(SuccessMessage, username, album.Name, permission));
        }
예제 #3
0
        public string Execute(string[] data)
        {
            int    albumId    = int.Parse(data[0]);
            string username   = data[1];
            string permission = data[2];

            if (!userSessionService.IsLoggedIn())
            {
                throw new ArgumentException("You are not logged in!");
            }

            bool isAlbumExist = albumService.Exists(albumId);

            if (!isAlbumExist)
            {
                throw new ArgumentException($"Album {albumId} not found!");
            }

            bool isUserExist = userService.Exists(username);

            if (!isUserExist)
            {
                throw new ArgumentException($"User {username} not found!");
            }

            int userId = userService.ByUsername <UserDto>(username).Id;

            bool isPermissionExist = Enum.TryParse(permission, out Role role);

            if (!isPermissionExist)
            {
                throw new ArgumentException($"Permission must be either “Owner” or “Viewer”!");
            }

            bool isRoleExist = albumRoleService.Exists(albumId, albumId);

            if (isRoleExist)
            {
                throw new ArgumentException("The Role already exist!");
            }

            albumRoleService.PublishAlbumRole(albumId, userId, role);

            string albumName = albumService.ById <AlbumDto>(albumId).Name;

            return($"Username {username} added to album\n{albumName} ({permission})");
        }
예제 #4
0
        // ShareAlbum <albumId> <username> <permission>
        // For example:
        // ShareAlbum 4 dragon321 Owner
        // ShareAlbum 4 dragon11 Viewer
        public string Execute(string[] data)
        {
            int    albumId    = int.Parse(data[0]);
            string username   = data[1];
            string permission = data[2];

            var albumExists = albumService.Exists(albumId);
            var userExists  = userService.Exists(username);

            if (!albumExists)
            {
                throw new ArgumentException($"Album {albumId} not found!");
            }
            else if (!userExists)
            {
                throw new ArgumentException($"User {username} not found!");
            }

            if (!userSessionService.IsLoggedIn())
            {
                throw new InvalidOperationException("Invalid credentials!");
            }

            bool isValid = Enum.TryParse(permission, true, out Role role);

            if (!isValid)
            {
                throw new ArgumentException($"Permission must be either “Owner” or “Viewer”!");
            }

            var userId    = userService.ByUsername <User>(username).Id;
            var albumName = albumService.ById <Album>(albumId).Name;

            AlbumRole albumRole = albumRoleService.PublishAlbumRole(albumId, userId, permission);

            return($"Username {username} added to album {albumName} ({albumRole.Role.ToString()})");
        }