コード例 #1
0
ファイル: UserService.cs プロジェクト: asanchezr/PSP
        /// <summary>
        /// Activate the new authenticated user with the PIMS datasource.
        /// If activating a service account, then the configuration must be provided to set the default attributes.
        /// </summary>
        /// <returns></returns>
        public PimsUser Activate()
        {
            this.User.ThrowIfNotAuthorized();

            var key      = this.User.GetUserKey();
            var username = this.User.GetUsername() ?? _options.ServiceAccount?.Username ??
                           throw new ConfigurationException($"Configuration 'Pims:ServiceAccount:Username' is invalid or missing.");
            var user   = this.Context.PimsUsers.FirstOrDefault(u => u.GuidIdentifierValue == key);
            var exists = user != null;

            if (!exists)
            {
                var givenName = this.User.GetFirstName() ?? _options.ServiceAccount?.FirstName ??
                                throw new ConfigurationException($"Configuration 'Pims:ServiceAccount:FirstName' is invalid or missing.");
                var surname = this.User.GetLastName() ?? _options.ServiceAccount?.LastName ??
                              throw new ConfigurationException($"Configuration 'Pims:ServiceAccount:LastName' is invalid or missing.");
                var email = this.User.GetEmail() ?? _options.ServiceAccount?.Email ??
                            throw new ConfigurationException($"Configuration 'Pims:ServiceAccount:Email' is invalid or missing.");
                var organization = this.User.GetOrganization(this.Context);

                this.Logger.LogInformation($"User Activation: key:{key}, email:{email}, username:{username}, first:{givenName}, surname:{surname}");

                var person = new PimsPerson()
                {
                    Surname = surname, FirstName = givenName
                };
                this.Context.PimsPeople.Add(person);
                this.Context.CommitTransaction();

                user = new PimsUser()
                {
                    GuidIdentifierValue     = key,
                    BusinessIdentifierValue = username,
                    Person    = person,
                    IssueDate = DateTime.UtcNow
                };
                this.Context.PimsUsers.Add(user);
                this.Context.CommitTransaction();

                var contactMethod = new PimsContactMethod()
                {
                    Person = person, Organization = organization, ContactMethodTypeCode = ContactMethodTypes.WorkEmail, ContactMethodValue = email
                };
                person.PimsContactMethods.Add(contactMethod);
                this.Context.CommitTransaction();
            }
            else
            {
                user.LastLogin = DateTime.UtcNow;
                this.Context.Entry(user).State = EntityState.Modified;
                this.Context.CommitTransaction();
            }

            if (!exists)
            {
                this.Logger.LogInformation($"User Activated: '{username}' - '{key}'.");
            }
            return(user);
        }
コード例 #2
0
        public PimsPerson AddPerson(PimsPerson person, bool userOverride)
        {
            person.ThrowIfNull(nameof(person));
            this.User.ThrowIfNotAuthorized(Permissions.ContactAdd);

            var createdPerson = _personRepository.Add(person, userOverride);

            _personRepository.CommitTransaction();

            return(GetPerson(createdPerson.Id));
        }
コード例 #3
0
        /// <summary>
        /// Get the concatenated full name of this organization
        /// </summary>
        /// <param name="organization"></param>
        /// <returns></returns>
        public static string GetFirstPersonFullName(this PimsOrganization organization)
        {
            PimsPerson person = organization.GetPersons().FirstOrDefault();

            if (person != null)
            {
                string[] names = { person.Surname, person.FirstName, person.MiddleNames };
                return(String.Join(", ", names.Where(n => n != null && n.Trim() != String.Empty)));
            }
            return(null);
        }
コード例 #4
0
        public PimsPerson UpdatePerson(PimsPerson person, long rowVersion)
        {
            person.ThrowIfNull(nameof(person));
            this.User.ThrowIfNotAuthorized(Permissions.ContactEdit);
            ValidateRowVersion(person.Id, rowVersion);

            var updatedPerson = _personRepository.Update(person);

            _personRepository.CommitTransaction();

            return(GetPerson(updatedPerson.Id));
        }
コード例 #5
0
        /// <summary>
        /// Create a new instance of a Contact, using entities
        /// </summary>
        /// <returns></returns>
        public static Entity.PimsContactMgrVw CreateContact(string id, bool isDisabled = false, PimsAddress address = null, PimsOrganization organization = null, PimsPerson person = null)
        {
            var contact = new Entity.PimsContactMgrVw()
            {
                Id               = id,
                Address          = address,
                FirstName        = person?.FirstName ?? "firstName",
                Surname          = person?.Surname ?? "surName",
                Summary          = organization?.OrganizationName ?? person?.GetFullName(),
                IsDisabled       = isDisabled,
                Organization     = organization,
                Person           = person,
                OrganizationName = organization?.OrganizationName ?? "organization name",
                MunicipalityName = address.MunicipalityName,
            };

            return(contact);
        }