473,379 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How do I assign one user to another user?

27
I have created a form where there's an option or 2 user types.

All the data collected will be posted to my msaccess table.

I have user java servlet to link the html to msaccess table so that user can view the table.

Now I need to complete another task which is I can allow 1st user to assign 2nd user to any existing 1st users available on my database.

I hope someone can advise me how to go about doing this.
Feb 1 '10 #1
12 2731
chaarmann
785 Expert 512MB
create a permission table. Give each user a role and store the user name and role there.
Then code some rules like an "administrator" can always assign a "normal user", but not the other way around.
So if 1st user tries to assign a 2nd user, query the permission table if he is allowed or not.
Feb 8 '10 #2
tangara
27
Hi Charmaine,

Could you please advise how create a permission table. I'm new in java and jsp so not sure how to create one. Is there any link you can point me to that can show me examples what you mentioned?
Feb 9 '10 #3
chaarmann
785 Expert 512MB
you can create a table with an SQL-command:
Expand|Select|Wrap|Line Numbers
  1. create table permission (user_name TEXT, role TEXT);
You can pass this SQL-command in java to your Database (access etc.) via JDBC or much better, use some library like Hibernate
Feb 9 '10 #4
tangara
27
Hi Chaarmann,

First, my apologies for addressing your name wrongly earlier.

Sorry, I'm still very confusing.

My question was how to do the assigning portion, assuming that the admin has the permission to do so.

Now, I already have a table/database which consist of :-

Name, email, ID, contact, Membeship Type

The membership types will have user1 and user2.

How do I let the admin person assigning user2 to user1, consider there are already many records there.

OK. Hope to hear from you soon.

Tks
Feb 10 '10 #5
chaarmann
785 Expert 512MB
You haven't given any example data. So I can only guess that the "Membeship" type column contains the role. (if not, just add a new column to the table).
So just grab the data from this table for user 1 and then for user 2 with the following SQL-command:
"select membeship from table where name='user1'". (or name='user2')
Then test if the roles are right (e.g. user1=administrator" and user2="guest") and if yes, process the requested task, else print an error.

This is role-based security. But If you want to map a user to one or more other users, you need individual security. Then you need to set up an extra table with the columns:
User-ID1, User-ID2.
So if a user with userid1 assigns other users with userid2 and userid3, you would simply add 2 rows to this table:
userid1, userid2
userid1, userid3
Feb 10 '10 #6
tangara
27
Hi Charmann,

I hope you don't mind I side track abit but this is related to the above question also.

Now, I tried to create session into my login page.

My table now will have userid, password and access.

Hence, I'll redirect the user according to the access which is admin or user1.

However, I'm a bit fuzzy about the session concepts. Could you take a look at my codes and advise me where I have done wrong?

My html code is:-
Expand|Select|Wrap|Line Numbers
  1. <form action="session.jsp">                   
  2.  
  3.                      <td>Userid</td>
  4.                     <td><input type="text" id="userid" name="login" ></td>
  5.                                <tr>
  6.                              <td>password</td>
  7.                     <td><input type="text" id="password" name="password" ></td>
  8.                 </tr>
  9.                 <tr>
  10.                              <td>accessType</td>
  11.                     <td><input type="text" id="access" name="access" ></td>
  12.                         </tr>
  13.                 <tr>
  14.                     <td align="right"><input type="submit" value="Submit" />
  15.                 </tr>
  16.                   </table>
  17.                  </form>
  18.   </body>
  19. </html>
  20.  
my session.jsp as follows;-

Expand|Select|Wrap|Line Numbers
  1. <%@page language="java" import ="java.sql.*" import="java.util.*" %>
  2.         <%  Connection conn = null;
  3.             PreparedStatement ps = null;
  4.             ResultSet rs = null;
  5.             Statement stmt = null;
  6.             PrintWriter out = null;
  7.             try {
  8.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  9.                 String url = "jdbc:odbc:sessionODBC";
  10.                 conn = DriverManager.getConnection(url);
  11.                 stmt = conn.createStatement();
  12.             } catch (Exception e) {
  13.                 System.out.println(e.getMessage());
  14.             }
  15.             rs = stmt.executeQuery("Select * from Staff");
  16.  
  17.             String accessPermission = rs.getString("access");
  18.             session.setAttribute("login", accessPermission;%>
  19.           rs.close();
  20.           stmt.close();
  21.           conn.close();
  22.         <% String accessType=(String)session.getAttribute("login");
  23.  
  24.         if(accessType==null){
  25.             response.sendRedirect("login.html");
  26.         }
  27.         else if(accessType.equals("member"){
  28.  
  29.             response.sendRedirect("xyz.jsp");
  30.         }
  31.         else{
  32.             out.printline("Found");
  33.         }
  34.         %>
  35.  
Many thanks.
Feb 14 '10 #7
chaarmann
785 Expert 512MB
Strange code. You are getting all entrries from table "staff" - but you should get only the entry of the current user. Then you don't loop through the table until you find your user, but just grab a column of the first record, ignoring all the other records. If the table is empty, it could be "null". Then you store the grabbed value into the session and retrieve it right afterwards. If it's null, you send him to a login-page, else somewhere else - That means, if you have no user inside your table, all are going to the login-page, but if you have at least one entry inside your table, all users that are trying to login are going to the page xyz.jsp or stay on the current, whatever you put inside this table at the first place.
And it doesn't matter in the slightest which username and password the users have put at the login-page session.jsp.

You are not grabbing any values from session.jsp with "response.getParameter(..)". You are not comparing entered password against stored password. You are not printing any error (redirect to error page) if the user is not allowed to enter. You are not destroying the current session if the user gave wrong credentials. So what's the use of the whole code???

By the way, you should never put the business-layer code inside a JSP!
This is a no-no! (Read about MVC; the JSP should only format your output!)
Make an Action-class in Java and make all the checkings there!
Use some ready-made Business-layer like Hibernate, so your code can run with all databases, instead of making the low-level database-calls yourself.)
Feb 17 '10 #8
tangara
27
Hi Chaarman,

Thanks for replying. Yes, my codes are not really correct. Anyway, I managed to find out the solution, after working on it for 3 days!!!

The hibernate thing is like a very distance subject to me. Would you be able to provide me with a link to learn basic stuff first?

Thank you again for your help.
Feb 18 '10 #9
chaarmann
785 Expert 512MB
@tangara
Can you please post your solution here, so that others with the same problem can benefit? I spent my time and knowledge to help you for free, so it's your turn now to help others .

And maybe I can help you even more by giving you some performance tips on your new code.

@tangara
Just google for "Hibernate". The first hit will go directly to "www.hibernate.org". The third link will go to wikipedia's article about Hibernate, for a rough overview. Under chapter "External links" at the bottom of this page there is also a link to a tutorial listed.
Feb 18 '10 #10
tangara
27
I just knew yesterday that I still have a bit of my code not working. That is, if people enter a wrong userid or password, the servlet returns a blank.

Could you help me out on this? Here's my code:-

Expand|Select|Wrap|Line Numbers
  1. public class Servlet1 extends HttpServlet {
  2.  
  3.  
  4.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  5.             throws ServletException, IOException {
  6.         response.setContentType("text/html;charset=UTF-8");      
  7.  
  8.     }
  9.  
  10.     @Override
  11.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  12.             throws ServletException, IOException {processRequest(request, response);
  13.     }
  14.  
  15.     @Override
  16.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  17.             throws ServletException, IOException {
  18.  
  19.         PrintWriter out = response.getWriter();
  20.  
  21.         String userid = request.getParameter("userid");
  22.         String password = request.getParameter("password");
  23.        // String access = request.getParameter("access");
  24.  
  25.         try {
  26.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  27.             Connection conn = DriverManager.getConnection("jdbc:odbc:sessionODBC");
  28.             Statement stmt = conn.createStatement();
  29.             String sql = "SELECT password, access FROM Staff WHERE userid='" + userid +
  30.                     "'" ;
  31.             ResultSet resultSet = stmt.executeQuery(sql);
  32.  
  33.             if (resultSet.next()){
  34.                 if  (!(resultSet.getString("userid")).equals(request.getParameter("null"))
  35.                         && (!(resultSet.getString("password")).equals(request.getParameter("null"))
  36.                         &&(resultSet.getString("password").equals(request.getParameter("password"))
  37.                     && (resultSet.getString("userid").equals(request.getParameter("userid"))))))
  38.                 {
  39.                     HttpSession session = request.getSession(true); // create a session
  40.                     String accessPermission = resultSet.getString("access");
  41.                     session.setAttribute("access", accessPermission);                    
  42.                     out.println("<html>");
  43.                     out.println("<head><title>Login Success</title></head>");
  44.                     out.println("<body>");
  45.                     out.println("<p>Login Success! Welcome " + userid);
  46.                     out.println("<a href=\"checkValid.jsp\">Click here to login</a>");
  47.                     out.println("</body></html>");                   
  48.                     } else {
  49.                     out.println("<html>");
  50.                     out.println("<head><title>No Such User</title></head>");
  51.                     out.println("<body>");
  52.                     out.println("<p>Login Failed! " + userid);
  53.                     out.println("<p>No such user exists, " + userid);
  54.                     out.println("</body></html>");
  55.                     out.println("<a href=\"login.html\">Click here to login</a>");
  56.                 out.close();
  57.                 conn.close();
  58.                     }}} catch  (Exception e) {
  59.             System.out.println("Error" + e.getMessage());
  60.                     }}
  61.  
Feb 19 '10 #11
chaarmann
785 Expert 512MB
1.) It returns blank, because the else-clause is missing for "if (resultset.next())". That's exactly the case if a user enters a wrong userId, so nothing is returned from database.So you should put appropriate error-messages (to be printed with out.println()). inside these else-clause.

2.) Your code is not secure, I can do an malicious code injection attack: if I type "'; delete from staff; select * from staff where id='" (pay attention to single quotes!) in your web page inside userid-field, your whole user-table got deleted. And I even can do more damage if I want instead, with a more complex SQL: delete all your tables! So you can avoid that in 2 ways: a.) replace single quotes with two single quotes, b.) use "prepared statements". They are faster anyway.

3.) the if-staement in line 34 is too complex! Why compare with userID? You know it's equal, because you searched in database with it in a way that it returns only those records where they are equal! And second, no need to call request.getParameter() again. You forgot that you already stored that value in "password" a few lines above.

4.) Your application is unsecure: you are transportating the plain password over the netword from the database! Everyone can read it with a network sniffer. You should only store the encrypted password inside your table.
And before you compare the passwords, you should also encrypt the password you got from the web page. So if both encrypted passwords are the same (web-page and database), you will grant access, else display an error.

5.) You should put all the HTML-code (Line 42 to 55) into an JSP-page. The rest is fine to stay here. (Separation of diplay and business logic, learn about MVC (= Model View Controller)). At least you could improve it by putting all the HTML-code inside a template that can be changed easily. Like:
Expand|Select|Wrap|Line Numbers
  1. String template="<html><head><body>...<p>Login Failed!  #userid <p>No such user exists,  #userid ...";
  2. template.replaceAll("#userid", userid);
  3. out.println(template);
  4.  
.
You could even store the template inside your database or filesystem and load it. Then there is no need to change the program if you want to change the HTML-code for another design later on.
Read about "freemarker templates" for automating this task. It's even better than JSP!

6.) quotation from my email above: "You are not destroying the current session if the user gave wrong credentials."
Feb 19 '10 #12
tangara
27
Hi Chaarman,

Thanks so much for your guidance above, I have changed my codes except I don't know about how to go about doing the encryption stuff. However, the funny thing is that I don't know why when the url, say display.jsp is pasted again, without proper login. It can be accessed. Could you kindly advise on this?

my code is as follows:-
Expand|Select|Wrap|Line Numbers
  1. public class Servlet2a extends HttpServlet {
  2.  
  3.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  4.             throws ServletException, IOException, SQLException {
  5.         response.setContentType("text/html;charset=UTF-8");
  6.         PrintWriter out = response.getWriter();
  7.         try {
  8.             String userid = request.getParameter("userid");
  9.             String password = request.getParameter("password");
  10.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  11.             Connection conn = DriverManager.getConnection("jdbc:odbc:sessionODBC");
  12.             String sql = "SELECT password, access FROM Staff WHERE userid= ? AND password = ?";
  13.  
  14.             PreparedStatement stmt = conn.prepareStatement(sql);
  15.             stmt.setString(1, userid);
  16.             stmt.setString(2, password);
  17.             ResultSet resultSet = stmt.executeQuery();
  18.  
  19.                  if (resultSet.next() != false) {
  20.                 String access = resultSet.getString("access");
  21.                 out.print("<p><h5>You have successfully logged in, " + userid + "</h5></p>");
  22.                 HttpSession session = request.getSession();
  23.                 session.setMaxInactiveInterval(3600);
  24.                 session.setAttribute("access", access);
  25.                 {
  26.                     out.print("<p><a href=\"displayData.jsp\">Update and Delete Data</a></p>");
  27.                 }
  28.             } else {
  29.                 out.print("<p>Wrong Userid or password.</p>");
  30.                 out.print("<p><a href=\"login.html\">Try again</a></p>");
  31.                 out.print("</body></html>");
  32.                 conn.close();
  33.             }
  34.         } catch (ClassNotFoundException cnfe) {
  35.             System.err.println("Error loading driver: " + cnfe);
  36.         } catch (SQLException sqle) {
  37.             System.err.println("Error with connection: " + sqle);
  38.         } finally {
  39.             out.close();
  40.         }
  41.     }
  42.  
And here's my displaypage.jsp
Expand|Select|Wrap|Line Numbers
  1. <%
  2.             String access = (String) session.getAttribute("access");
  3.             if (access == null) {
  4.                 response.sendRedirect("login.html");
  5.             }
  6.  
  7.             %>
  8. <%-- Display page --%>
  9. <%         Connection conn = null;
  10.             PreparedStatement ps = null;
  11.             ResultSet rs = null;
  12.             Statement stmt = null;
  13.  
  14.             try {
  15.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  16.                 String url = "jdbc:odbc:sessionODBC";
  17.                 conn = DriverManager.getConnection(url);
  18.                 stmt = conn.createStatement();
  19.             } catch (Exception e) {
  20.                 System.out.println(e.getMessage());
  21.             }
  22.             rs = stmt.executeQuery("Select * from MemberParticulars3");
  23. %>
  24. <html>
  25.     <head>
  26.                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  27.     </head>
  28.     <body>
  29.              <h5>Members Record</h5>
  30.             <table border ="1" cellspacing="0" cellspacing="0" align="center" class="bordered">
  31.                 <tr><td><b>Member No</b></td>
  32.                     <td><b>Name</b></td>
  33.                     <td><b>NRIC</b></td>
  34.                     <td><b>Email</b></td>
  35.                     <td><b>Address</b></td>
  36.                     <td><b>Gender</b></td>
  37.                     <td><b>Type</b></td>
  38.                     <td><b>Remarks</b></td>
  39.                     <td><b>Contact</b></td>
  40.                 </tr>
  41.  
  42.                 <% int no = 1;
  43.                             while (rs.next()) {
  44.                                 int id = rs.getInt("ID");
  45.                 %>
  46.                 <tr>
  47.                     <td><%= id%> </td>
  48.                     <td> <%=rs.getString("strFullNameME")%> </td>
  49.                     <td><%=rs.getString("strNRICNOME")%> </td>
  50.                     <td><%=rs.getString("strEmailME")%> </td>
  51.                     <td><%=rs.getString("strAddressME")%> </td>
  52.                     <td><%=rs.getString("strGenderME")%> </td>
  53.                     <td><%=rs.getString("strTypeME")%> </td>
  54.                     <td><%=rs.getString("strRemarksME")%> </td>
  55.                     <td><%=rs.getString("strContactME")%> </td>
  56.  
  57.                     <% if (access.equals("admin")) {
  58.                                                         out.print("<td><a href=\"Update.jsp?ID=" + id + "\">Update</a></td>");
  59.                                                     }%>
  60.  
  61.                     <% if (access.equals("admin")) {
  62.                                                         out.print("<td><a href=\"DeleteMember.jsp?ID=" + id + "\">Delete</a></td>");
  63.                                                     }%>
  64.                     <td>
  65.                         <%no++;%>
  66.                     </td></tr> <%}
  67.                                 rs.close();
  68.                                 stmt.close();
  69.                                 conn.close();
  70.                     %>
  71.             </table>
  72.  
  73.             <tr>
  74.                 <td>Total Number of Members: <%=no - 1%>
  75.                     <br />
  76.                     To return to login page : "<a href="login.html">Click here</a>"</td></tr>
  77.              <tr>
  78.             <td>
  79.                To logout : "<a href="logout.jsp">Click here</a>"</td></tr>
  80.  
  81.         <% if (access.equals("admin")) {
  82.                         out.print("<td><a href=\"AddMember.jsp\">Click here to Register Member</a></td>");
  83.                     } else {
  84.                         out.print("<a href=\"AddMember.jsp\"><h5>Register as Member or Volunteer click here</h5></a>");
  85.                     }%>    
  86. </body>
  87. </html>
  88.  
Feb 21 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Halfdan Holger Knudsen | last post by:
Hey here's a relatively simple question...the code is for a mock-up passwd, usrname dict storagesystem. Why doesn't the variable enc behave globally (the admin function turns up a "local var...
2
by: Megan | last post by:
Hi everybody- I have 2 tables, Hearings and Rulings. Both have primary keys called, CaseID, that are autonumbers. I don't want both tables to have the same autonumber. For example, if Hearings...
0
by: JJ_377 | last post by:
The following doesn't assign value to the dropdownlist - WHY? ___________________________________________________________________ In a user control (ascx named USACustomer) : Public Property...
5
by: MN | last post by:
Hello, I have a customer table and another table that I need to prepopulate with special customer IDs, unique and not sequential. Is there a way to configure Access to assign the customer ID to...
4
by: philin007 | last post by:
Hi , I have the following javascript codes: ****************************************** <script language="JavaScript"> <!-- .... ..... if (nextRow >5) {
42
by: blisspikle | last post by:
I tried closely copying some code that I found on this group for assigning a type at runtime, but I cannot get it to work. Can someone see what is wrong with my logic? Thanks, Private Sub...
0
by: =?Utf-8?B?QmxhZGltaXI=?= | last post by:
I need to know, if is possible assign one USB port to user's session Windows XP. I have this question because need restrict some user's account for use webcam. If this is possible, how I do it? ...
7
by: trakal | last post by:
Hello everybody, i create a stored producedure in Oracle that will get the user name who connect to Oracle database. For exemple, i declare a variable "o_user" and i want to assign the value of the...
5
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
9
by: raylopez99 | last post by:
Just an observation: pens for drawing lines in Win Forms are tricky when assignment is inside the paint handler. inside of the Paint handler, but not inside a "using" brace (that is, outside of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.