Disable JButton when not logged in - java

I am creating a mini project. My project is a library management system.
How can I disable the issue button in advanced search if I am logged in by clicking Anonymus?
Or how can I enable the issue button if logged in as administrator?

You can enable / disable buttons using the setEnabled() method. You will have to figure out the code for the boolean that needs to be passed to the method.

Basically, what you need to do is to call setEnabled(isAdministrator()) on your Issues button. For this, you'll have to implement an isAdministrator() method, which should check if a user is logged in and if that user is the/an administrator. How to check this depends on the implementation of your login dialog and on how you administer your users. Normally you'd have some kind of database holding all users and (encrypted) passwords as well as a flag telling if a certain user is an administrator (or you can have a separate table with roles or permissions and a many-to-many relation between the user table and the roles or permissions table but for a mini-project this is probably not needed).

Just make a boolean variable initialized as false when you validate through username and password change it to true, buttons have a jButton.setVisibility("true or false"); this can either be true or false but adding the boolean variable there will determine whether its a registered user or an anonymous client :)

Related

how to save checkbox state in android

I'm working on an android cooking app (using java) and the homepage has a recycler view populated with recipes which users can like (similar to FaceBook posts). The like button is a checkbox, what is the best way to save the state of the like checkbox for every recipe, so when the user signs out of the app and sign in again they will not like the same recipe more than one time.
Is using SharedPreference a good idea in this situation?
**im using MySql as a database and firebase is not used.
You could definitely do it using SharedPreferences - but I believe this is more ideally designed for 'preferences' of an application rather than 'application' behaviour state.
You might want to have a look at : https://developer.android.com/topic/libraries/architecture/saving-states?authuser=1
Here you will see some options of how to serialize and persist UI states to memory. I have not actually used something like this before - in the past only using SharedPreferences.
You could of course also create your own storage method, or solution but why bother when ones already exist.
Since you are already using a MySQL database, your application is already grabbing the rows from the DB to show them, so why not add another column called 'checked' which is a boolean type. Then when you get your recipes you get the 'checked' variable, and if it is true, then set your UI state of the checkbox to checked, otherwise false :)
The benefit of this approach is that if your app became 'hybrid' and you wanted a website for it too - the data on the database becomes centralised, meaning your persisted user state is the same on mobile as it is web experience, which is a nice benefit!

Firebase user data storage issue

I am working with Firebase Authentication and while coding I realised that I can use User user = FirebaseAuth.getInstance().getCurrentUser; to fetch user data in any activity through out the application if user successfully logged in but I am confused that whether I can store user data in sharedPreferences after user login or I can use User user = FirebaseAuth.getInstance().getCurrentUser; way to get user data in whichever activity I want to. So which method is preferable to use firebase user data. And if there any other method you have to suggest then you are always welcome. Thanks in advance.
The user data is limit to login info like user id or email address by using FirebaseAuth.getInstance().getCurrentUser().
If it's enough to use it for you, that's ok.
In order to get the data which saved to sharedpreferences, you can define PreferencesUtils class.
The class is singleton or has static members.
If so, I think you can call this members anywhere.
I hope it helps.. If you any questions, let me know.
Use always firebase user instance, because it gives to you an updated user instance. if you are going to save user data in sharedPreferences, you should always save user details in sharedPreferences when you update the user data.
I would use an AuthStateListener to listen to possible changes to the authenticated user, and persist changes to that user object only when they might change, according to that listener. It's probably not worthwhile to continually ping currentUser for changes when you know it can only change when response to the listener callback.

How do I separate keywords for 2 different users?

I have couple of different users on my app. There are certain keywords associated with these users based on their selections and I want to make them such that, user1's kselected keywords are not displayed for user 2 when he logs on the same device.
Here's the basic code :
Boolean firstuserlogin= Sharedpreferences.getinstance().firstlogin;
Boolean sameuserlogin = !Sharedpreferences.getinstance().differentuser;
Boolean differentuserlogin = Sharedpreferences.getinstance().differentuser;
if(firstuserlogin && differentuserlogin) {
//Please note: firsttime user is also considered a new/different user
m_keyword = Sharedpreferences.getinstance().getkeyword1();
}else if(sameuserlogin){
m_keyword = Sharedpreferences.getinstance().getkeyword1();
}else if(differentuser){
m_keyword = Sharedpreferences.getinstance().getkeyword2();
}else{
m_keyword = Sharedpreferences.getinstance().getkeyword1();
}
The issue is I am not able to get it to work as expected.Theoretically: Say a user 1 logs in ,he's considered first user and at the same time a new/different user, until he logs out.His keyword settings which he selects during his session are saved as expected. Now, Say he logs out and logs in again , he should be able to see his keyword settings and be considered as the "sameuser" since he logged in before, unless some other user with a different id logs in on the same device, in which case, it should show all new default keyword(which is lets say keyword2 in this case ) and should not show the previous users selections.Currently based on the above logic I implemented it seems to go haywire, sometimes it works, and sometimes it doesnt recognize the user as a different user, but same user AND VICE VERSA. is there a better and compact way I can restrict and implement this logic?
Thanks!
Well, if you insist on using shared preferences, you can use a unique key(email,username,userId anything) and merge it with the keyword and save to share preferences. You will need a special character to seperate the unique key and keyword (somekind of key-value pair)
for example if we use id as uniqkey and ## as token character. keywordA for the keyword you should save as "userid1##keywordA) and read it back from preferences. After you get it. You should split it via ##.
But for such operations shared preferences is not the best place, you should use a database since you have multiple users. Android supports sqlite database built in. You can see a tutorial here
Good luck

Check if user Name exist

What i want todo with Java and Javascript:
If a user try to register an Account, after he write the Login name and klick in the next field, there should be an immediately check, if the Login Name already exist.
My Question is now, what is the best performance way.
I know, how i can check the username in the database, that is no Problem.
But is it possible to cache the List of users in a Application wide variable ?
If yes, how or where should i create a such variable ? I use tomcat as server.
But no idea how i can do that.
Or is it just fine, todo a check on the DB Server.
I want something similar like the Registration from hotmail
Thanks
Two ways to do it (and not thinking very hard). First - before loading the page, on the server side retrieve all user names, put the in a list and put the list in the request. Now you have all your user in the page and can check if the entered name exists (must do it in javascript). The second method - after typing the name make an ajax call to the server and check in DB if exists. Hope this helps.
It's not a good idea to cache all login names. First because at every http session, you need to refresh the whole cache. Second because it's possible to have multiple http sessions (multiple user trying to create an account) and you need to refresh the whole cache to verify new registrations login names. Third it's not a good practice to store temporary the whole user names table in such a variables.. imagine you have 10000000 login names!
With a cache, if two users want to register at the same time, and enters the same user login, both user login pass the validation!
Just query your database with an ajax request or a servlet and make sure your login name column has an index!

Spring logout problem

I have an application which is validating only one user should use the application at a time. For that we are putting a column in DB table. By default the column value is 'Inactive'. When the user logged in, column value will be changed to 'active'. Once the user properly logged out, the column value will be changed to 'inactive' and the session gets an end. But when the user close the window improperly, the logout code is not working. I tried with onunload but the problem with the onunload is since we are using tiles, each time when we click a link log out is happening. So can anyone help me how to do it while closing the window directly?
Thanks in advance...
You should not be keeping an entry in DB to track whether the user is online or not. Instead you have to keep all the user names in the application context and whenever the user logs in, you have to check if the user name is already in the application context. If not, add it to the context, else the user has already logged in.
Having a DB column to know if the user is active or not is a very bad idea. If at all this is used, you have to call an event using javascript every time you close the browser window. Also it might so happen that because of some network problem, the window close event will not be intercepted by the server resulting in the user being still shown as active in the DB

Categories

Resources