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.

Results 1 to 5 of 5

Thread: creating admin and normal users

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default creating admin and normal 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...


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: creating admin and normal users

    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?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: creating admin and normal users

    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/

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: creating admin and normal users

    can you throw me one example too @aussiemcgr?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: creating admin and normal users

    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/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    sadaharu (July 2nd, 2012)

Similar Threads

  1. [SOLVED] Is This Closing To Code Normal?
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 8th, 2012, 01:08 AM
  2. normal distribution random number algorithm
    By bondage in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 25th, 2011, 12:59 AM
  3. Generate users id
    By sante in forum Algorithms & Recursion
    Replies: 5
    Last Post: October 6th, 2011, 11:29 AM
  4. To Create and Admin an database
    By JavaNoob89 in forum Object Oriented Programming
    Replies: 2
    Last Post: December 10th, 2010, 09:15 AM
  5. Replies: 1
    Last Post: May 3rd, 2010, 01:03 PM