public RoleContract[] GetUserRoles(IHasUserId user)
 {
     try
     {
         using (var records = RepositoryFactory.GetUserProfile())
         {
             records.Discriminator = new UserProfileDiscriminator()
             {
                 Filter = p => p.UserId == user.UserId
             };
             var profile = records.FirstOrDefault() as DB.UserProfile;
             if (profile == null)
             {
                 throw new Xxception("User was not found by id")
                       {
                           Tag = new XDocument(new XElement("UserId", user.UserId))
                       }
             }
             ;
             var ret = profile.webpages_UsersInRoles;
             return(ret.Select(r => new RoleContract()
             {
                 Role = new RoleImplementor()
                 {
                     RoleId = r.RoleId, RoleName = r.webpages_Role.RoleName
                 }
             }).ToArray());
         }
     }
     catch (Exception ex)
     {
         HandleMyException(ex);
         return(null);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Looks up user locations
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static IEnumerable <LocationModel> GetLocations(this IHasUserId user)
        {
            var db = new MembershipCashierDL.Access.LowLevelAccess();

            return(db.FindLocation(null, null, new UserProfileDiscriminator()
            {
                Filter = x => x.UserId == user.UserId
            }).Select(l => l.Location.ToLocationModel()));
        }
 public CompletePrincipal(IIdentity sourceIdentity, IHasUserId sourceUser)
 {
     if (typeof(CompleteIdentity).IsInstanceOfType(sourceIdentity))
     {
         identity = sourceIdentity as CompleteIdentity;
     }
     else
     {
         identity = new CompleteIdentity(sourceIdentity, sourceUser);
     }
 }
        public void AddRoleToUser(IHasUserId user, RoleContract[] roles, int modifiedByUserId)
        {
            try
            {
                lock (MDB)
                {
                    var profile = MDB.UserProfiles.FirstOrDefault(p => p.UserId == user.UserId);
                    if (profile == null)
                    {
                        throw new Xxception("User was not found by id")
                              {
                                  Tag = new XDocument(new XElement("UserId", user.UserId))
                              }
                    }
                    ;
                    foreach (var newRole in roles)
                    {
                        if (!profile.webpages_UsersInRoles.Any(r => r.RoleId == newRole.Role.RoleId || r.webpages_Role.RoleName == newRole.Role.RoleName))
                        {
                            var dbRole = MDB.webpages_Roles.FirstOrDefault(r => r.RoleId == newRole.Role.RoleId || r.RoleName == newRole.Role.RoleName);
                            if (dbRole == null)
                            {
                                dbRole = new DB.webpages_Role()
                                {
                                    RoleId = newRole.Role.RoleId, RoleName = newRole.Role.RoleName
                                };
                                MDB.webpages_Roles.InsertOnSubmit(dbRole);
                            }

                            var dbUserInRole = new DB.webpages_UsersInRole()
                            {
                                RoleId = newRole.Role.RoleId, webpages_Role = dbRole
                            };
                            profile.webpages_UsersInRoles.Add(dbUserInRole);

                            MDB.webpages_UsersInRolesAudits.InsertOnSubmit(new DB.webpages_UsersInRolesAudit()
                            {
                                Action           = Convert.ToByte(AddRemoveOptions.Add),
                                ModificationTime = DateTime.UtcNow,
                                ModifiedBy       = modifiedByUserId,
                                RoleId           = newRole.Role.RoleId,
                                UserId           = user.UserId
                            });
                        }
                    }
                    MDB.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                HandleMyException(ex);
            }
        }
Esempio n. 5
0
        internal static void InitSuperAdmin(string userName)
        {
            if (userName == SecurityUnified.Constants.SYSTEM)
            {
                throw new Exception("System role is handled separately");
            }

            if (userName.Contains("admin"))
            {
                //foreach (string role in AppGlobal.SuperAdminRoles.Where(r => r != Constants.SYSTEM))
                //{
                if (!string.IsNullOrWhiteSpace(userName) &&
                    Roles.Provider.GetUsersInRole(SecurityUnified.Constants.SUPER_ADMIN).Count() < 2)      //First admin is the system
                {
                    var profile = ServiceAccessor.SecurityDb.FindUserProfile(new UserProfileDiscriminator()
                    {
                        Filter = p => p.UserName == userName
                    }).Single();
                    IHasUserId updatedBy = null;
                    if (SessionGlobal.CurrentUser != null)
                    {
                        updatedBy = SessionGlobal.CurrentUser.Identity;
                    }
                    if (updatedBy == null)
                    {
                        updatedBy = new UserProfileImplementor();
                        ServiceAccessor.SecurityDb.FindUserProfile(new UserProfileDiscriminator()
                        {
                            Filter = p => p.UserName == SecurityUnified.Constants.SYSTEM
                        }).Single().UserProfile.CopyTo(updatedBy);
                    }

                    ServiceAccessor.SecurityDb.AddRoleToUser(profile.UserProfile, new RoleContract[] { }, updatedBy.UserId);
                }
                //}
            }
        }
Esempio n. 6
0
 public OwnerPrincipal(IIdentity sourceIdentity, IHasUserId sourceUser)
     : base(sourceIdentity, sourceUser)
 {
 }
Esempio n. 7
0
 public CompleteIdentity(IIdentity sourceIdentity, IHasUserId sourceUser)
 {
     this.identity = sourceIdentity;
     this.user     = sourceUser;
 }
Esempio n. 8
0
 public static void CopyTo(this IHasUserId from, IHasUserId to, bool allowDefaultValues = true)
 {
     to.UserId = from.UserId;
 }
 public static bool IsEqual(this IHasUserId from, IHasUserId to)
 {
     return(to.UserId == from.UserId);
 }