password.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @name Password Module
  3. * @description Controls the access to password-protected albums and photos.
  4. * @author Tobias Reich
  5. * @copyright 2014 by Tobias Reich
  6. */
  7. password = {
  8. value: "",
  9. set: function(albumID) {
  10. var buttons,
  11. params;
  12. buttons = [
  13. ["Set Password", function() {
  14. if (visible.album()) {
  15. album.json.password = true;
  16. view.album.password();
  17. }
  18. params = "setAlbumPassword&albumID=" + albumID + "&password=" + hex_md5($(".message input.text").val());
  19. lychee.api(params, function(data) {
  20. if (data!==true) lychee.error(null, params, data);
  21. });
  22. }],
  23. ["Cancel", function() {}]
  24. ];
  25. modal.show("Set Password", "Set a password to protect '" + album.json.title + "' from unauthorized viewers. Only people with this password can view this album. <input class='text' type='password' placeholder='password' value=''>", buttons);
  26. },
  27. get: function(albumID, callback) {
  28. var passwd = $(".message input.text").val(),
  29. params;
  30. if (!lychee.publicMode) callback();
  31. else if (album.json&&album.json.password==false) callback();
  32. else if (albums.json&&albums.json.content[albumID].password==false) callback();
  33. else if (!albums.json&&!album.json) {
  34. // Continue without password
  35. album.json = {password: true};
  36. callback("");
  37. } else if (passwd==undefined) {
  38. // Request password
  39. password.getDialog(albumID, callback);
  40. } else {
  41. // Check password
  42. params = "checkAlbumAccess&albumID=" + albumID + "&password=" + hex_md5(passwd);
  43. lychee.api(params, function(data) {
  44. if (data===true) {
  45. password.value = hex_md5(passwd);
  46. callback();
  47. } else {
  48. lychee.goto("");
  49. loadingBar.show("error", "Access denied. Wrong password!");
  50. }
  51. });
  52. }
  53. },
  54. getDialog: function(albumID, callback) {
  55. var buttons;
  56. buttons = [
  57. ["Enter", function() { password.get(albumID, callback) }],
  58. ["Cancel", lychee.goto]
  59. ];
  60. modal.show("<a class='icon-lock'></a> Enter Password", "This album is protected by a password. Enter the password below to view the photos of this album: <input class='text' type='password' placeholder='password' value=''>", buttons, -110, false);
  61. },
  62. remove: function(albumID) {
  63. var params;
  64. if (visible.album()) {
  65. album.json.password = false;
  66. view.album.password();
  67. }
  68. params = "setAlbumPassword&albumID=" + albumID + "&password=";
  69. lychee.api(params, function(data) {
  70. if (data!==true) lychee.error(null, params, data);
  71. });
  72. }
  73. }