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...
Printable View
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.
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.
Code java: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(); } }
Code java: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; } }
Code java: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; } }