Base solution for your next web application
Open Closed

Add a new property to the user ou session about this user #2289


User avatar
0
malcon created

I have a very simple question, but I do not know how to proceed. I need to identify in the user which company he belongs to (company is not a tenant) I tried using session, implementing a new class with a new property

IocManager.Register<IAbpSession, MySession>(DependencyLifeStyle.Transient);
public class MySession : IAbpSession, ITransientDependency
    {
        public IAbpSession _AbpSession;

        public int? CurrentCompany { get; set; }

        public MySession()
        {

        }

        public int? ImpersonatorTenantId
        {
            get { return _AbpSession.ImpersonatorTenantId; }
        }


...

However this way I can not access the properties via DI.

Also I have not seen an easy way to change the User class and always record the company which the user is logged in.

Can someone help me?


8 Answer(s)
  • User Avatar
    0
    malcon created

    I'll try to explain my problem in more detail, it may be that Abp can help me using some already existing structure that, I'm not seeing.

    I have 3 types of users that will use the platform:

    • System administrator (me)
    • Company User
    • Counters (third party users who may see one or more companies, however not all of the system)

    The use of the tenant was limited to the use by the accountants, since they must see 1 or more companies, but without being able to see all, that is, they can not be admin, just me.

    For this, I created a structure Companies vs. Users, thus controlling who has access to a particular company.

    The user, when entering the system, selects which company wants to see the data, this way I force the filter and I only bring the data referring to the company that he selected during that session.

    This is the question in relation to the above question, how to know and filter the data of which company the user selected.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    First of all, you need to define your own session but you need to inject it instead of IAbpSession when you need to access CurrentCompany, because IAbpSession interface does not have it. For an example you can check this <a class="postlink" href="https://gist.github.com/hikalkan/67469e05475c2d18cb88">https://gist.github.com/hikalkan/67469e05475c2d18cb88</a>.

    Then, when you write CompanyId (or any company inforamtion you like) to your session, you can use it for filtering entities. In order to do that, you need to define a custom data filter, for example IMustHaveCompany or IMayHaveCompany. You can check this document for <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Data-Filters#DocDefineCustomFilters">http://www.aspnetboilerplate.com/Pages/ ... tomFilters</a>

  • User Avatar
    0
    malcon created

    Thank you, that's exactly what I needed!

  • User Avatar
    0
    malcon created

    When I use the property after a Login, everything happens perfectly.

    LoginResult.Identity.AddClaim (new Claim ("Company", CompanyId.ToString ()));
    

    I tried to use _userManager.AddClaim, in another part of the code, where the user can select another company related to it, it starts to add the values in the table AbpUserClaims, but recording the wrong information.

    Claim myClaim = new Claim ("type", "value");
        _userManager.AddClaim (_AbpSession.GetUserId (), myClaim);
    

    ClainType receives the value, and the ClaimValue is null, generating problems for the next user login.

    What is the correct way to do this operation?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    What is the value of _AbpSession.GetUserId () when you debug ?

  • User Avatar
    0
    malcon created

    I'll send parts of my source, I think it's easier. MySession Class

    public class MySession : IAbpSession, ITransientDependency
        {
            public IAbpSession _AbpSession;
    
            public MySession(IAbpSession AbpSession)
            {
                 _AbpSession=AbpSession;
            }
    
            public int? ImpersonatorTenantId
            {
                get { return _AbpSession.ImpersonatorTenantId; }
            }
    
            public long? ImpersonatorUserId
            {
                get { return _AbpSession.ImpersonatorUserId; }
            }
    
            public Abp.MultiTenancy.MultiTenancySides MultiTenancySide
            {
                get { return _AbpSession.MultiTenancySide; }
            }
    
            public int? TenantId
            {
                get { return _AbpSession.TenantId; }
            }
    
            public long? UserId
            {
                get { return _AbpSession.UserId; }
            }
    
            public string FormaturaSelecionada
            {
                get
                {
                    var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                    if (claimsPrincipal == null)
                    {
                        return null;
                    }
    
                    var formaturaClaim = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == "FormaturaAtual");
                    if (formaturaClaim == null || string.IsNullOrEmpty(formaturaClaim.Value))
                    {
                        return null;
                    }
    
                    return formaturaClaim.Value;
                }
                set
                {
                    var claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
                    if (claimsPrincipal == null)
                    {
                        throw new Exception("erro");
                    }
    
                    claimsPrincipal.Identities.First().AddClaim(new Claim("FormaturaAtual", value));
                }
            }
        }
    

    The user when selecting the Graduation, through the screen below, calls the controler FormaturaSelect, sending the new ID which must be set in the session variable.

    public async Task<JsonResult> FormaturaSelect(int id, string returnUrl = "")
            {
                Debug.WriteLine(string.Format("----Check selected value ----"));
                Debug.WriteLine(string.Format("Step 1: Current Value in _mySession = [{0}]", _mySession.FormaturaSelecionada));
                Debug.WriteLine(string.Format("Step 2: Add value id= [{0}]", id));
                _mySession.FormaturaSelecionada = id.ToString();
                Debug.WriteLine(string.Format("Step 3: Current Value in _mySession = [{0}]", _mySession.FormaturaSelecionada));
    
    
                //continue...
    
            }
    

    I put several debugs in the code, below the results. First execution: ----Check selected value ---- Step 1: Current Value in _mySession = [] Step 2: Add value id= [1] Step 3: Current Value in _mySession = [1]

    Second executionL ----Check selected value ---- Step 1: Current Value in _mySession = [] Step 2: Add value id= [2] Step 3: Current Value in _mySession = [2]

    The value between the two requests is lost. But it can be seen that it adds to the current Claim

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry for my late response, did you solve this problem ? I couldn't find anything yet.

    I will investigate according to your answer.

    Thanks.

  • User Avatar
    0
    malcon created

    No, not yet solved. I'm temporarily recording the selected id in a field within the Users entity which I've extended, but this is costly in terms of performance to the database.