Hello,
I'm developing a project in netbeans 7.1... I'd like to know how to go about the coding such that the administrator has access to all parts of the application while a normal user has restricted access... Thnx...
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello,
I'm developing a project in netbeans 7.1... I'd like to know how to go about the coding such that the administrator has access to all parts of the application while a normal user has restricted access... Thnx...
Is this a website (i.e. a java bean based thing or is it an applet or is it javascript?
I have a code, somewhere, that shows how to do something like that, at least tell if the user is an Admin or not, and also, once I can find the specifics, it's a lot of code to search through, it also would go to different web pages and let people have different things that they could or could not do.
Is that what you're after?
If you are talking about having just an application with a log-in and users, I would suggest creating two classes: User and Admin. The User class would have protected fields for username and password and some public methods for access. Admin would extend from User and override the User's methods for access with some higher-level access.
Because of how inheritance works, you can then treat all users (both normal users and admins) as just Users, and when you call the methods for access, it will use the Admin's version of the access methods if the user is an admin, and the normal User's version of the access methods if the user is just a normal user.
If it would help to see an example, I could throw one together for you.
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/
can you throw me one example too @aussiemcgr?
Here is a VERY basic example. There are three files.
*Hello is the test file. Two users are created, one admin and the other just a basic user. The username and the password for the basic user is just user, while the password for the admin is just admin. You will be prompted for both, and then it just prints out 3 privileges.
*User is the basic user. User has an authenticate method, and 3 privilege methods (each representing some arbitrary level of access). User only has access to level 1, so hasPermission1() is the only one of the 3 methods that returns true.
*Admin is an administrator user. Admin inherits the authenticate method from User, and then overrides the 3 privilege methods it inherits from User. Admin has full access, so all of its privilege methods will return true.
From here, it is just a question of how well you understand inheritance. If you have any questions, feel free to ask and I'll help the best I can.
import java.util.Scanner; /** * */ public class Hello { /** * @param args */ public static void main(String[] args) { User[] users = new User[] {new User("user","user"),new Admin("admin","admin")}; Scanner in = new Scanner(System.in); System.out.println("Enter Username:"); String username = in.nextLine(); System.out.println("Enter Password:"); String password = in.nextLine(); for(User user:users) { if(user.authenticate(username, password)) { System.out.println("Privilege #1: "+user.hasPermission1()); System.out.println("Privilege #2: "+user.hasPermission2()); System.out.println("Privilege #3: "+user.hasPermission3()); break; } } in.close(); } }
public class User { protected String userName; protected String password; /** * @param un Username * @param pw Password */ public User(String un, String pw) { this.userName = un; this.password = pw; } /** * @param un Username * @param pw Password * @return <b>true</b> if correct account, <b>false</b> otherwise */ public boolean authenticate(String un, String pw) { return un.equals(this.userName) && pw.equals(this.password); } /** * @return true */ public boolean hasPermission1() { return true; } /** * @return false */ public boolean hasPermission2() { return false; } /** * @return false */ public boolean hasPermission3() { return false; } }
public class Admin extends User { /** * @param un Username * @param pw Password */ public Admin(String un, String pw) { super(un, pw); } @Override public boolean hasPermission1() { return true; } @Override public boolean hasPermission2() { return true; } @Override public boolean hasPermission3() { return true; } }
NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:
When asking for help, please follow these guidelines to receive better and more prompt help:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
2. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly.
Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/
sadaharu (July 2nd, 2012)