0

i have the following problem.

i hava a java class

 public class A {
    login()
    getRights()
    methodA()
    methodB()
    methodC()
   }

now i have the following scenario. I create an instance of class A. and now i want to check with the login function wheter i'm logged in. If not, the object don't have the rights to call method a, b and c. After that i want to check with the getRights() function wheter the object have the rights to call the method b and c. Is there a smart implementation? otherwise i have to inherit from class a and overwrite the blocked functions ?

4
  • If not the object don't have the rights to call method a, b and c. Exactly Commented Feb 23, 2017 at 13:20
  • I'm not sure I understand you correctly but you could either let the methods check for rights right at the start or use some sort of interceptor to check permissions (depends on the frameworks you're using, the general approach would be AOP). That being said having authentication, authorization and business logic in one class is very odd design. Commented Feb 23, 2017 at 13:21
  • i dont use any framework Commented Feb 23, 2017 at 13:24
  • ok i think. i have to check the permission at the start of any method Commented Feb 23, 2017 at 13:27

2 Answers 2

1

You can call login() before calling methodA(), or you can write a call to login() inside methodA() so that the code inside methodA() can't be run without running login() first. Then if the object calls methodA() but doesn't return the right value from login() you can throw an exception before going any farther. Then do the same thing with getRights() inside B and C.

Sign up to request clarification or add additional context in comments.

Comments

0

You could throw an exception if method a, b and c were called but you were not logged in. You just need to use variables to set either that you are logged in or not, and with that throw exceptions or not, and getRights just uses that variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.