User specific setting in rails - ruby-on-rails-3

I am developing a Ruby on Rails application. In my app I have a setting on the welcome page where user can select the language (by default is English). Where should I declare this variable and where should I change it from default once the user changes it?
I tried declaring a config variable in development.rb, but since it is global for the applicaition, if user A changes the language, user B will also see the changes done by user A.
I need a setting which is user specific. In other words: one session specific. When I open the website it should initialize to the default and when I change it, the application should not apply those changes to others.

If you "need a setting which is user specific" you could define a session variable:
session[:language]

Related

Plone does not fully recognize manage_setLocalRoles changes

I have a script that is using manage_setLocalRoles to assign a specific role to certain users specified. See below for snippet
context.manage_setLocalRoles(username, (‘Editor’, ‘Reviewer’))
context.reindexObjectSecurity()
After that script runs, you can go to the Sharing tab of the page and see the user specified with Reviewer and Editor checked. However, that user does not have any of the rights that go with those roles.
For testing, I have performed the following checks
context.getMyRolesInContext() does not return any of the roles set above.
context.get_local_roles_for_userid(username) only displays the roles set above, and not any global roles set (when testing as site manager and the like).
context.portal_membership.checkPermission(“Modify portal content”) returns a False.
With Editor role I should have Modify portal content permission. I did verify in the workflow of the content type to make sure the roles are set correctly for the permissions in each state. And as a double check, I ran this script on the content.
username = context.getCurrentUserName()
roles = context.rolesOfPermission('Modify portal content')
member = context.portal_membership.getMemberById(username)
for role in roles:
if role['selected']!='':
print role['name']
print member.has_role(role['name'], context)
return printed
This was my result:
Editor
0
Manager
1
Reviewer
0
Site Administrator
0
Those results are from my site manager role, but after I ran the manage_setLocalRoles on my user to add Editor and Reviewer.
Any thoughts as to why the roles aren’t recognized everywhere? Or am I using the wrong process for what I am trying to accomplish?
NOTE - I have already looked at this answer, and as my code reflects I am already doing what it suggests.
EDIT
Adding versions per comment. We have quite a few add-ons, but none of them seem like they would be related. Mostly jQuery integrations, and types like PFG, True Gallery, FullCalendar. We are using a custom theme and all custom content types and workflows. Excluding the content type I am currently creating for this project both the theme and all other content types were created before I got here. The types are Archetypes extensions.
OS - Red Hat Enterprise Linux Server release 5.11 (Tikanga)
Plone - 4.3.2
Products.ATContentTypes - 2.1.13
AccessControl - 3.0.8
The problem boiled down to case sensitivity. Our users are fed from an Active Directory plugin. Some of the usernames are all caps, and others are all lower. Somehow the username being used in my set roles command were always lowercase. As a result, user was getting the roles, when it should have been USER.
My Solution was to first get the actual member from portal_membership, then grab the username from the member.
pm = getToolByName(self, 'portal_membership')
user = pm.getMemberById(username)
self.manage_setLocalRoles(member.getUserName(), ('Reviewer', 'Editor'))
self.reindexObjectSecurity()
Apparently getMemberById() is not case sensitive, and it returns the correct username in the correct case. So I was just missing that extra check/filter.

Yii RBAC, Role change in runtime

I am building up a dynamic RBAC system for Yii and I don't know how to handle this problem:
The moderators can change the roles of the Users, furthermore the User can change it too by getting a different qualification (let's say achievement, so s/he can do more stuff and it can happen both ways).
What happens, when the role is changed Backwards (a role with less right) or Forwards (a role with more right) when s/he is logged in? Cannot access the functions he just got the right to use? Or can still access the functions until a logout/relog action?
Thanks your help in advance.
The effect of changing the authorization assignment will be inmediate.
Only the successive calls to IWebUser::checkAccess() issued in the same request may return cached values, since the default implementation of IWebUser, i.e. CWebUser, uses a static attribute to cache the calculated permissions.
To clarify the procedure, you will be calling IAuthManager::revoke() on the old permissions and IAuthManager::assign() on the new ones.
Edit
Sometimes you store session information through the IWebUser::setState() method; if the state of the currently logged user shall change along with the permissions, e.g. you store the current user's role name, you must take this into account and either call IWebUser::clearState() or IWebUser::logout() followed by IWebUser::login() –the latter also clears the cached permissions in the CWebUser implementation.
CWebUser::_access is declared private, so you will have to declare a new attribute if you want to override the default implementation.

How to authenticate a session in a website

When a user logs in to a website what method is the best to authenticate the session? For example does setting a variable in $_SESSION that is checked and if is set the user logged in, work? I was reading this tutorial and they have if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) and if this returns true the user is shown the contents of the page. Is this how it's done?
Yup, that's the general idea.
After setting $_SESSION['LoggedIn'] to TRUE elsewhere (on the login page) you'll check the $_SESSION['LoggedIn'] to see if it's TRUE if so, display the content
I agree with #relentless. However I prefer if you store the information inside the database.
Assuming that you have a table called user with a tinyint column logged_in. Whenever the user has successfully login update the column value to 1. Upon logging out change it to 0.
Yes and I would recommend you looking a little into PHP and some of the predefined variables like $_POST and $_SESSION here http://php.net/manual/en/reserved.variables.php . And functions like isset() and empty() and maybe some html forms.

Application Settings scope - user vs application

If I create a user level application setting and bind it to a text box on a form, then type something in the textbox, the value is automatically saved and when the application is launched again the value appears in the textbox. This doesn't happen when I scope the setting as application.
Why are application scoped settings not saved automatically like user scoped settings are?
If this is by design, how can I manually save these settings and load them at runtime?
In short, application scoped settings can't be changed at runtime while user scoped settings are designed be read/written at runtime. There are a number of alternatives to using application scoped settings:
Use user scoped settings
This post recommends looking into the ConfigurationManager class.
If you don't like user scoped settings because they are not written to the application directory you can implementing your own SettingsProvider and have them written where ever you'd like
This post has some recommendations on how to best manage settings. Finally, here is Microsoft's documentation on the settings architecture if you're interested in extending theirs or rolling your own.

Handling log-in / log-out via Objective-c

Having a real problem with this one...Tried using cookies to store variables, etc. but no luck.
Writing an iPhone app where the User has to log in. There is an HTTPS call to get the person's userid, which is used practically everywhere else in the app, so that either has to be stored in a global variable or a cookie (for sending messages to other users, etc.)
I tried the cookie route, but am having great difficulty storing (and retriving) a user ID in a cookie.
The User should be able to then close out of the app and then reboot it and have the app retain their User ID as well, so I'm not sure global variables are necessarily the solution to this.
Are there any best practices or suggestions?
Persistent storage of settings should be handled via the user defaults - see the Introduction to User Defaults.