Ejemplo n.º 1
0
        /// <summary>
        /// Returns a User based on the guid
        /// </summary>
        /// <param name="context"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static Public.Models.User GetUserBySmUserId(this IDynamicsClient _dynamicsClient, string guid)
        {
            Guid id = new Guid(guid);

            Public.Models.User user = null;
            var contact             = _dynamicsClient.GetContactByExternalId(id.ToString());

            if (contact != null)
            {
                user = new Public.Models.User();
                user.FromContact(contact);
            }

            return(user);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a User based on the guid
        /// </summary>
        /// <param name="context"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static async Task <Public.Models.User> GetUserByGuid(this IDynamicsClient _dynamicsClient, string guid)
        {
            Guid id = new Guid(guid);

            Public.Models.User user = null;
            var contact             = _dynamicsClient.GetContactById(id);

            if (contact != null)
            {
                user = new Public.Models.User();
                user.FromContact(contact);
            }

            return(user);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Load User from database using their userId and guid
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userId"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static async Task <Public.Models.User> LoadUser(this IDynamicsClient _dynamicsClient, string userId, IHeaderDictionary Headers, ILogger _logger, string guid = null)
        {
            Public.Models.User          user    = null;
            MicrosoftDynamicsCRMcontact contact = null;
            Guid userGuid;

            if (!string.IsNullOrEmpty(guid))
            {
                user = await _dynamicsClient.GetUserByGuid(guid);
            }

            if (user == null)
            {
                _logger.LogInformation(">>>> LoadUser for BCEID.");
                if (Guid.TryParse(userId, out userGuid))
                {
                    user = _dynamicsClient.GetUserBySmUserId(userId);
                    if (user != null)
                    {
                        _logger.LogInformation(">>>> LoadUser for BCEID: user != null");
                        // Update the contact with info from Siteminder
                        var contactVM = new Public.ViewModels.Contact();
                        contactVM.CopyHeaderValues(Headers);
                        _logger.LogInformation(">>>> After reading headers: " + Newtonsoft.Json.JsonConvert.SerializeObject(contactVM));
                        MicrosoftDynamicsCRMcontact patchContact = new MicrosoftDynamicsCRMcontact();
                        patchContact.CopyValues(contactVM);
                        try
                        {
                            _dynamicsClient.Contacts.Update(user.ContactId.ToString(), patchContact);
                        }
                        catch (OdataerrorException odee)
                        {
                            _logger.LogError("Error updating Contact");
                            _logger.LogError("Request:");
                            _logger.LogError(odee.Request.Content);
                            _logger.LogError("Response:");
                            _logger.LogError(odee.Response.Content);
                            // fail if we can't create.
                            throw (odee);
                        }
                    }
                }
                else
                { //BC service card login
                    _logger.LogInformation(">>>> LoadUser for BC Services Card.");
                    string externalId = GetServiceCardID(userId);
                    contact = _dynamicsClient.GetContactByExternalId(externalId);

                    if (contact != null)
                    {
                        _logger.LogInformation(">>>> LoadUser for BC Services Card: contact != null");

                        /*
                         * user = new User();
                         * user.FromContact(contact);
                         *
                         * // Update the contact and worker with info from Siteminder
                         * var contactVM = new Public.ViewModels.Contact();
                         * var workerVm = new Public.ViewModels.Worker();
                         * contactVM.CopyHeaderValues(Headers);
                         * workerVm.CopyHeaderValues(Headers);
                         * MicrosoftDynamicsCRMcontact patchContact = new MicrosoftDynamicsCRMcontact();
                         * MicrosoftDynamicsCRMadoxioWorker patchWorker = new MicrosoftDynamicsCRMadoxioWorker();
                         * patchContact.CopyValues(contactVM);
                         * patchWorker.CopyValues(workerVm);
                         * try
                         * {
                         *  string filter = $"_adoxio_contactid_value eq {contact.Contactid}";
                         *  var workers = _dynamicsClient.Workers.Get(filter: filter).Value;
                         *  foreach (var item in workers)
                         *  {
                         *      _dynamicsClient.Workers.Update(item.AdoxioWorkerid, patchWorker);
                         *
                         *  }
                         *  _dynamicsClient.Contacts.Update(user.ContactId.ToString(), patchContact);
                         * }
                         * catch (OdataerrorException odee)
                         * {
                         *  _logger.LogError("Error updating Contact");
                         *  _logger.LogError("Request:");
                         *  _logger.LogError(odee.Request.Content);
                         *  _logger.LogError("Response:");
                         *  _logger.LogError(odee.Response.Content);
                         *  // fail if we can't create.
                         *  throw (odee);
                         * }
                         */
                    }
                }
            }

            if (user == null)
            {
                return(null);
            }

            if (guid == null)
            {
                return(user);
            }


            if (!user.ContactId.ToString().Equals(guid, StringComparison.OrdinalIgnoreCase))
            {
                // invalid account - guid doesn't match user credential
                return(null);
            }

            return(user);
        }