How to get user id of logged in user - yii

I want to ask one question, that if I want to get the id of any user in config/main.php file of yii, then how can I get this ? If any one has idea please tell ?
Thanks

Your config/main.php is used to build your application, hence it is not possible to get data in there that is only available after Yii::app() is available.
But you can get your user id by Yii::app()->user->id;
You can use that everywhere, for example in Controller->init() that will execute before your SiteController is ran.
You can still set your language after you config.
Yii::app()->language = Helper::getLanguageFromUser(Yii::app()->user->id);
Ofc Helper is a fictive class here, it can be any thing. See an example here.

Related

How to access a custom module from the website (for portal users) [odoo13]

I have a custom module for user to upload some information and files
I can use forms and make the module accessible from the website but I need them to be able to edit there recored also, exactly like if they are internal user but only for y custom module
anyone knows how to do that thank u very much in advance.
edit
after reading the comment of #KevalMehta
I think my question should be how to update a record from a form
this is my code to create a new record in my module
request.env['manager.create_company_link'].sudo().create(kw)
so first I think I need the id of the record I need to update before how to update it
I thing #KevalMehta is referring to me using .sudo() how to give the right access

Authentication between modules in Zend Framework

Currently I'm using ZF3 with two modules.
The first module handles the authentication of the users.
The second module should use informations about the authentication status of the users.
In Detail: The second module should verify if the user is logged in.
Depending on that status it should allow routing to certain Controllers.
E.g. in the template of the first module, I can use: $this->auth()->isLoggedIn()
But - of course - I'm not able to use auth() in the second modules templates.
I'm still learning ZF, so I don't know how to inform the second module about the status of the authentication.
I guess it has something to do with ZFs ServiceManager or PluginManager, but I'm not sure.
Any help would be appreciated...
Thanks to some explanations on https://www.tutorialspoint.com/zend_framework/zend_framework_service_manager.htm I solved it!
In FooControllerFactory::__invoke I used the Container to find the AuthManager
$authManager = $container->get(\Vendor\Auth\Manager::class);
Then, I returned it:
return new FooController($entityManager, $FooManager, $authManager);
(entityManager is from doctrine, FooManager is the manager for this model)
In FooController.php I added the authManager to the constructor
$this->authManager = $authManager;
Now I can get e.g. a username in any FooAction! :)
$this->authManager->getIdentity()->username
(replace "username" with the correct name of the user_id row)

Kohana 3 auth username as number

I want to use numbers as username in Kohana Auth. For example, username 100001?
While adding new user Kohana returns me error: ORM_Validation_Exception [ 0 ]: Failed to validate array
Is is possible to user numbers as username in Kohana?
EDIT: This answer looks simpler and better than mine, but try to understand it at all.
You need to extend User Model, I'll help you using auth with the ORM driver.
Steps to extend User Model:
If you didn't yet, configure Auth module to use orm and create a database table with the fields you want. Here is a good example of how to doing it (It's an old tutorial using ko3.1 but you can still learn from it). PS.: you can have any columns at the 'users' table and you don't need to have the 'username' column if you do not want.
Open and read carefully this file: MODULES/orm/classes/model/auth/user.php (It's self documented and I hope you understand it. If not, stop reading this answer here and read the kohana docs. Some shortcuts: Auth - Kohana User Guide, Auth (orm) methods, addons:auth
Copy the file (don't edit the original) to APPPATH/classes/model/auth/user.php and edit it how you want. Some functions that you may like to edit are: rules, filters and unique_key (<- useful). Be creative, you also can add custom functions.
Test and change whatever else needed.
You can change the login method to works as you like. You can set login by e-mail, make a custom validation method or parse values before saving in the database (see public function filters()). This is helpful for whatever you try to do with auth module using ORM... But... if you really don't want to use ORM, you can build your own driver, learn how.
I made this some time ago in kohana 3.2 but I think you won't get problems with 3.3. If you still have questions, this question on kohana forum may help.

Passing the arguments to a website

Well the title is tricky. I was not sure if this has been already there and how to put it.
Example:
Lets suppose my site is accessible:
http://mysite.com
if mysite does additions: with 2 inputs
Now If the end user need to pass an argument to site like this (so i load the page like this):
http://mysite.com/argument1/argument2
so it should be able to thus go to the result directly: arg1+arg2
This brings to the question:when user types this, how can i retreive argument1 and argument2 and load my site according to that? Is it possible? If yes, Any client site programming solutions?
Thanks in advance.
Found the solution after searching a lot. The correct term for this process is URL-routing. We can overcome this by using #.
www.mysite.com/#/argu1/argu2
Then in the document.ready() we can read the url in the following manner:
var url = window.location;
More sophisticated way is to use the "backbone.js" for the routing. The documentation on the link explains everything
Backbone.js routing

yii: serve a file when logged in

I have pdf files in a htaccess-protected directory that I want to show to some users in Yii application.
How is best to solve that? I understand it should be done inside Yii, not with a separate script as it needs to check if a valid user is logged in.
Access control filters
You would do your appropriate security checking (user is in group, or has permission or whatever) and then you would use readfile or something similar.
Do you have code already that isn't working? Or did you just need help with what approach to take?
Thanks!
I got some idea from this link:
http://harrybailey.com/2011/07/yii-rewrite-files-or-images-for-download-or-display/
In short: made a new actionfunction into controller and in that function sending headers and readfile.