Exemple #1
0
        private void CreateAdminUser()
        {
            var    adminId  = Guid.NewGuid();
            string password = this._securityManager.EncodePassword(Resources.DefaultValues.AdministratorPassword);
            var    admin    = new User(adminId, Resources.DefaultValues.AdministratorEmail, Resources.DefaultValues.AdministratorName);

            admin.PreferredCulture = StrixPlatform.DefaultCultureCode;
            this._dataSource.Save(admin);

            var security = new UserSecurity(admin.Id);

            security.Password = password;
            security.Approved = true;
            this._dataSource.Save(security);

            var session = new UserSessionStorage(admin.Id);

            this._dataSource.Save(session);

            var userRole = new UserInRole(this._adminRole, admin.Id, DateTime.Now, null);

            admin.Roles = new List <UserInRole>()
            {
                userRole
            };
        }
        public void AddUserToRole(Guid groupId, Guid userId, string roleName, DateTime?startDate = null, DateTime?endDate = null)
        {
            if (groupId == Guid.Empty || userId == Guid.Empty || string.IsNullOrWhiteSpace(roleName))
            {
                throw new ArgumentException("Please specify a group id, user id and/or a role name.");
            }

            var roleId     = this.GetRoleId(roleName, "user", "add");
            var userEntry  = this._dataSource.Find <UserInRole>(new object[] { groupId, roleId, userId });
            var groupEntry = this._dataSource.Find <GroupInRole>(new object[] { groupId, roleId });

            if (groupEntry == null)
            {
                if (!StrixPlatform.User.IsInMainGroup)
                {
                    var message = string.Format("Trying to add user {0} to role {1} to which his company has no access.", userId, roleName);
                    Logger.LogToAudit(AuditLogType.IllegalOperation.ToString(), message);
                    throw new StrixMembershipException(message);
                }
                else
                {
                    groupEntry = new GroupInRole(StrixPlatform.ApplicationId, roleId);
                    this._dataSource.Save(groupEntry);
                }
            }

            startDate = startDate.HasValue && startDate != new DateTime() ? startDate.Value : DateTime.Now;
            startDate = startDate < groupEntry.StartDate ? groupEntry.StartDate : startDate;

            endDate = endDate == new DateTime() ? null : endDate;
            endDate = endDate.HasValue && groupEntry.EndDate.HasValue && endDate.Value > groupEntry.EndDate ? groupEntry.EndDate : endDate;
            endDate = endDate == null && groupEntry.EndDate.HasValue ? groupEntry.EndDate : endDate;

            if (userEntry == null)
            {
                if (groupEntry.MaxNumberOfUsers.HasValue && groupEntry.CurrentNumberOfUsers >= groupEntry.MaxNumberOfUsers.Value)
                {
                    throw new StrixMembershipException(string.Format("No licenses left for role {0}", roleName));
                }

                userEntry = new UserInRole(groupEntry, userId, startDate.Value, endDate);
                groupEntry.CurrentNumberOfUsers++;
            }
            else
            {
                userEntry.StartDate = startDate.Value;
                userEntry.EndDate   = endDate;
            }

            this._dataSource.Save(userEntry);
            _userRoles.Clear();
        }
Exemple #3
0
        public void InitAdminUser()
        {
            var saveChanges = false;

            this._adminUser = this._dataSource.Query <User>("Roles.GroupRole.Role").FirstOrDefault(u => u.Email.ToLower() == Resources.DefaultValues.AdministratorEmail.ToLower());

            if (this._adminUser == null)
            {
                this.CreateAdminUser();
                saveChanges = true;
            }
            else if (!this._adminUser.Roles.Any(r => r.GroupRole.Role.Name.ToLower() == PlatformConstants.ADMINROLE.ToLower()))
            {
                var userRole = new UserInRole(this._adminRole, this._adminUser.Id, DateTime.Now, null);
                this._adminUser.Roles.Add(userRole);
                saveChanges = true;
            }

            if (saveChanges)
            {
                this._dataSource.SaveChanges();
            }
        }
        private void CreateAdminUser()
        {
            var adminId = Guid.NewGuid();
            string password = this._securityManager.EncodePassword(Resources.DefaultValues.AdministratorPassword);
            var admin = new User(adminId, Resources.DefaultValues.AdministratorEmail, Resources.DefaultValues.AdministratorName);
            admin.PreferredCulture = StrixPlatform.DefaultCultureCode;
            this._dataSource.Save(admin);

            var security = new UserSecurity(admin.Id);
            security.Password = password;
            security.Approved = true;
            this._dataSource.Save(security);

            var session = new UserSessionStorage(admin.Id);
            this._dataSource.Save(session);

            var userRole = new UserInRole(this._adminRole, admin.Id, DateTime.Now, null);
            admin.Roles = new List<UserInRole>() { userRole };
        }
        public void InitAdminUser()
        {
            var saveChanges = false;
            this._adminUser = this._dataSource.Query<User>("Roles.GroupRole.Role").FirstOrDefault(u => u.Email.ToLower() == Resources.DefaultValues.AdministratorEmail.ToLower());

            if (this._adminUser == null)
            {
                this.CreateAdminUser();
                saveChanges = true;
            }
            else if (!this._adminUser.Roles.Any(r => r.GroupRole.Role.Name.ToLower() == PlatformConstants.ADMINROLE.ToLower()))
            {
                var userRole = new UserInRole(this._adminRole, this._adminUser.Id, DateTime.Now, null);
                this._adminUser.Roles.Add(userRole);
                saveChanges = true;
            }

            if (saveChanges)
            {
                this._dataSource.SaveChanges();
            }
        }
        public void AddUserToRole(Guid groupId, Guid userId, string roleName, DateTime? startDate = null, DateTime? endDate = null)
        {
            if (groupId == Guid.Empty || userId == Guid.Empty || string.IsNullOrWhiteSpace(roleName))
            {
                throw new ArgumentException("Please specify a group id, user id and/or a role name.");
            }

            var roleId = this.GetRoleId(roleName, "user", "add");
            var userEntry = this._dataSource.Find<UserInRole>(new object[] { groupId, roleId, userId });
            var groupEntry = this._dataSource.Find<GroupInRole>(new object[] { groupId, roleId });

            if (groupEntry == null)
            {
                if (!StrixPlatform.User.IsInMainGroup)
                {
                    var message = string.Format("Trying to add user {0} to role {1} to which his company has no access.", userId, roleName);
                    Logger.LogToAudit(AuditLogType.IllegalOperation.ToString(), message);
                    throw new StrixMembershipException(message);
                }
                else
                {
                    groupEntry = new GroupInRole(StrixPlatform.ApplicationId, roleId);
                    this._dataSource.Save(groupEntry);
                }
            }

            startDate = startDate.HasValue && startDate != new DateTime() ? startDate.Value : DateTime.Now;
            startDate = startDate < groupEntry.StartDate ? groupEntry.StartDate : startDate;

            endDate = endDate == new DateTime() ? null : endDate;
            endDate = endDate.HasValue && groupEntry.EndDate.HasValue && endDate.Value > groupEntry.EndDate ? groupEntry.EndDate : endDate;
            endDate = endDate == null && groupEntry.EndDate.HasValue ? groupEntry.EndDate : endDate;

            if (userEntry == null)
            {
                if (groupEntry.MaxNumberOfUsers.HasValue && groupEntry.CurrentNumberOfUsers >= groupEntry.MaxNumberOfUsers.Value)
                {
                    throw new StrixMembershipException(string.Format("No licenses left for role {0}", roleName));
                }

                userEntry = new UserInRole(groupEntry, userId, startDate.Value, endDate);
                groupEntry.CurrentNumberOfUsers++;
            }
            else
            {
                userEntry.StartDate = startDate.Value;
                userEntry.EndDate = endDate;
            }

            this._dataSource.Save(userEntry);
            _userRoles.Clear();
        }