PageRenderTime 50ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/MovieTicketing/src/ipd12/movieticketing/DlgLoginAdmin.java

https://bitbucket.org/RobertoDiBi/java3-project
Java | 284 lines | 230 code | 26 blank | 28 comment | 12 complexity | c735f953830c1a26a30faca3101b9bc7 MD5 | raw file
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package ipd12.movieticketing;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import javax.swing.JOptionPane;
  10. import org.jasypt.util.password.BasicPasswordEncryptor;
  11. /**
  12. *
  13. * @author 1796174
  14. */
  15. public class DlgLoginAdmin extends javax.swing.JDialog
  16. {
  17. public static final Pattern VALID_EMAIL_ADDRESS_REGEX
  18. = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  19. public DlgLoginAdmin(java.awt.Frame parent, boolean modal)
  20. {
  21. super(parent, modal);
  22. initComponents();
  23. }
  24. /**
  25. * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
  26. */
  27. @SuppressWarnings("unchecked")
  28. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  29. private void initComponents()
  30. {
  31. jLabel11 = new javax.swing.JLabel();
  32. jLabel12 = new javax.swing.JLabel();
  33. pfPass = new javax.swing.JPasswordField();
  34. jLabel13 = new javax.swing.JLabel();
  35. tfEmail = new javax.swing.JTextField();
  36. btLogin = new javax.swing.JButton();
  37. btCancel = new javax.swing.JButton();
  38. jLabel14 = new javax.swing.JLabel();
  39. pfAdminPass = new javax.swing.JPasswordField();
  40. setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  41. jLabel11.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
  42. jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  43. jLabel11.setText("Log in");
  44. jLabel11.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  45. jLabel12.setText("Username (e-mail):");
  46. jLabel13.setText("Password:");
  47. btLogin.setText("Log in");
  48. btLogin.addActionListener(new java.awt.event.ActionListener()
  49. {
  50. public void actionPerformed(java.awt.event.ActionEvent evt)
  51. {
  52. btLoginActionPerformed(evt);
  53. }
  54. });
  55. btCancel.setText("Cancel");
  56. btCancel.addActionListener(new java.awt.event.ActionListener()
  57. {
  58. public void actionPerformed(java.awt.event.ActionEvent evt)
  59. {
  60. btCancelActionPerformed(evt);
  61. }
  62. });
  63. jLabel14.setText("Admin password:");
  64. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  65. getContentPane().setLayout(layout);
  66. layout.setHorizontalGroup(
  67. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  68. .addGroup(layout.createSequentialGroup()
  69. .addContainerGap()
  70. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  71. .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 337, javax.swing.GroupLayout.PREFERRED_SIZE)
  72. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  73. .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
  74. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
  75. .addComponent(btLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
  76. .addGroup(layout.createSequentialGroup()
  77. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  78. .addComponent(jLabel12)
  79. .addComponent(jLabel13)
  80. .addComponent(jLabel14))
  81. .addGap(18, 18, 18)
  82. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  83. .addComponent(tfEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
  84. .addComponent(pfPass)
  85. .addComponent(pfAdminPass))))
  86. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  87. );
  88. layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCancel, btLogin});
  89. layout.setVerticalGroup(
  90. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  91. .addGroup(layout.createSequentialGroup()
  92. .addContainerGap()
  93. .addComponent(jLabel11)
  94. .addGap(26, 26, 26)
  95. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  96. .addComponent(jLabel12)
  97. .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  98. .addGap(18, 18, 18)
  99. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  100. .addComponent(jLabel13)
  101. .addComponent(pfPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  102. .addGap(18, 18, 18)
  103. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  104. .addComponent(jLabel14)
  105. .addComponent(pfAdminPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  106. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
  107. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  108. .addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
  109. .addComponent(btLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
  110. .addContainerGap())
  111. );
  112. pack();
  113. setLocationRelativeTo(null);
  114. }// </editor-fold>//GEN-END:initComponents
  115. private void btLoginActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btLoginActionPerformed
  116. {//GEN-HEADEREND:event_btLoginActionPerformed
  117. String email = tfEmail.getText();
  118. String pass = new String(pfPass.getPassword());
  119. String adminpass = new String(pfAdminPass.getPassword());
  120. boolean userExists = false;
  121. BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
  122. if (email.equals(""))
  123. {
  124. JOptionPane.showMessageDialog(this, "Please enter your e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
  125. return;
  126. } else if (!validate(email))
  127. {
  128. JOptionPane.showMessageDialog(this, "Please enter a valid e-mail address.", "Missing information", JOptionPane.WARNING_MESSAGE);
  129. return;
  130. }
  131. for (User u : Globals.users)
  132. {
  133. if (u.email.equals(email))
  134. {
  135. userExists = true;
  136. if (passwordEncryptor.checkPassword(pass, u.password))
  137. {
  138. if (!adminpass.equals(Globals.adminPass))
  139. {
  140. JOptionPane.showMessageDialog(this, "Wrong admin password.", "Wrong password", JOptionPane.WARNING_MESSAGE);
  141. return;
  142. }
  143. this.dispose();
  144. Globals.movieGUI.lblUserStatus.setText("Logged in as: " + u.getEmail() + " (Admin)");
  145. Globals.movieGUI.meRegister.setVisible(false);
  146. Globals.movieGUI.meLogin.setVisible(false);
  147. Globals.movieGUI.meUser.setVisible(true);
  148. Globals.movieGUI.meUser.setText(u.firstName);
  149. Globals.movieGUI.meAdminTools.setVisible(true);
  150. Globals.currentUser = u;
  151. Globals.userLogged = true;
  152. return;
  153. }
  154. }
  155. }
  156. if (userExists)
  157. {
  158. JOptionPane.showMessageDialog(this, "Wrong password. Please try again.", "Wrong password", JOptionPane.WARNING_MESSAGE);
  159. } else
  160. {
  161. Object[] options
  162. =
  163. {
  164. "Register", "Try again"
  165. };
  166. int decision = JOptionPane.showOptionDialog(null,
  167. "This email is not registered yet. Would you like to register an account?",
  168. "No record found",
  169. JOptionPane.YES_NO_OPTION,
  170. JOptionPane.WARNING_MESSAGE,
  171. null, //do not use a custom Icon
  172. options, //the titles of buttons
  173. options[0]); //default button title
  174. if (decision == JOptionPane.YES_OPTION)
  175. {
  176. this.dispose();
  177. DlgRegister dlgRegister = new DlgRegister(Globals.movieGUI, true);
  178. dlgRegister.pack();
  179. dlgRegister.setVisible(true);
  180. }
  181. }
  182. }//GEN-LAST:event_btLoginActionPerformed
  183. private void btCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btCancelActionPerformed
  184. {//GEN-HEADEREND:event_btCancelActionPerformed
  185. this.dispose();
  186. }//GEN-LAST:event_btCancelActionPerformed
  187. public static boolean validate(String emailStr)
  188. {
  189. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
  190. return matcher.find();
  191. }
  192. /**
  193. * @param args the command line arguments
  194. */
  195. public static void main(String args[])
  196. {
  197. /* Set the Nimbus look and feel */
  198. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  199. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  200. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  201. */
  202. try
  203. {
  204. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
  205. {
  206. if ("Nimbus".equals(info.getName()))
  207. {
  208. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  209. break;
  210. }
  211. }
  212. } catch (ClassNotFoundException ex)
  213. {
  214. java.util.logging.Logger.getLogger(DlgLoginAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  215. } catch (InstantiationException ex)
  216. {
  217. java.util.logging.Logger.getLogger(DlgLoginAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  218. } catch (IllegalAccessException ex)
  219. {
  220. java.util.logging.Logger.getLogger(DlgLoginAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  221. } catch (javax.swing.UnsupportedLookAndFeelException ex)
  222. {
  223. java.util.logging.Logger.getLogger(DlgLoginAdmin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  224. }
  225. //</editor-fold>
  226. //</editor-fold>
  227. //</editor-fold>
  228. //</editor-fold>
  229. /* Create and display the dialog */
  230. java.awt.EventQueue.invokeLater(new Runnable()
  231. {
  232. public void run()
  233. {
  234. DlgLoginAdmin dialog = new DlgLoginAdmin(new javax.swing.JFrame(), true);
  235. dialog.addWindowListener(new java.awt.event.WindowAdapter()
  236. {
  237. @Override
  238. public void windowClosing(java.awt.event.WindowEvent e)
  239. {
  240. System.exit(0);
  241. }
  242. });
  243. dialog.setVisible(true);
  244. }
  245. });
  246. }
  247. // Variables declaration - do not modify//GEN-BEGIN:variables
  248. private javax.swing.JButton btCancel;
  249. private javax.swing.JButton btLogin;
  250. private javax.swing.JLabel jLabel11;
  251. private javax.swing.JLabel jLabel12;
  252. private javax.swing.JLabel jLabel13;
  253. private javax.swing.JLabel jLabel14;
  254. private javax.swing.JPasswordField pfAdminPass;
  255. private javax.swing.JPasswordField pfPass;
  256. private javax.swing.JTextField tfEmail;
  257. // End of variables declaration//GEN-END:variables
  258. }