Example #1
0
        internal static bool HasAllPermissions(this IAuthSession session, ICollection <string> requiredPermissions,
                                               IAuthRepository authRepo, IRequest req)
        {
            var allPerms = session.GetPermissions(authRepo);

            if (requiredPermissions.All(allPerms.Contains))
            {
                return(true);
            }

            if (session.HasRole(RoleNames.Admin, authRepo))
            {
                return(true);
            }

            session.UpdateFromUserAuthRepo(req, authRepo);

            allPerms = session.GetPermissions(authRepo);
            if (requiredPermissions.All(allPerms.Contains))
            {
                req.SaveSession(session);
                return(true);
            }
            return(false);
        }
        public bool HasAllRoles(IAuthSession session, IAuthRepository authRepo)
        {
            if (session == null)
                return false;

            return this.RequiredRoles.All(x => session.HasRole(x, authRepo));
        }
        public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary <string, string> authInfo)
        {
            base.OnAuthenticated(authService, session, tokens, authInfo);

            //Populate all matching fields from this session to your own custom User table
            var user = session.TranslateTo <User>();

            user.Id = int.Parse(session.UserAuthId);
            user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()
                ? CreateGravatarUrl(session.Email, 64)
                : null;

            foreach (var authToken in session.ProviderOAuthAccess)
            {
                if (authToken.Provider == FacebookAuthProvider.Name)
                {
                    user.FacebookName      = authToken.DisplayName;
                    user.FacebookFirstName = authToken.FirstName;
                    user.FacebookLastName  = authToken.LastName;
                    user.FacebookEmail     = authToken.Email;
                }
                else if (authToken.Provider == TwitterAuthProvider.Name)
                {
                    user.TwitterName = authToken.DisplayName;
                }
                else if (authToken.Provider == GoogleOpenIdOAuthProvider.Name)
                {
                    user.GoogleUserId   = authToken.UserId;
                    user.GoogleFullName = authToken.FullName;
                    user.GoogleEmail    = authToken.Email;
                }
                else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
                {
                    user.YahooUserId   = authToken.UserId;
                    user.YahooFullName = authToken.FullName;
                    user.YahooEmail    = authToken.Email;
                }
                else if (authToken.Provider == StsAuthProvider.Name)
                {
                    user.StsUserName = authToken.UserName;
                }
            }

            if (AppHost.Config.AdminUserNames.Contains(session.UserAuthName) &&
                !session.HasRole(RoleNames.Admin))
            {
                using (var assignRoles = authService.ResolveService <AssignRolesService>())
                {
                    assignRoles.Post(new AssignRoles {
                        UserName = session.UserAuthName,
                        Roles    = { RoleNames.Admin }
                    });
                }
            }

            //Resolve the DbFactory from the IOC and persist the user info
            authService.TryResolve <IDbConnectionFactory>().Run(db => db.Save(user));
        }
        public override void OnAuthenticated(
            IServiceBase authService,
            IAuthSession session,
            IOAuthTokens tokens,
            Dictionary<string, string> authInfo)
        {
            base.OnAuthenticated(authService, session, tokens, authInfo);

            //Populate all matching fields from this session to your own custom User table
            var user = session.TranslateTo<User>();
            user.Id = int.Parse(session.UserAuthId);
            //user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty() ? CreateGravatarUrl(session.Email, 64) : null;

            foreach (var authToken in session.ProviderOAuthAccess)
            {
                //    //if (authToken.Provider == FacebookAuthProvider.Name)
                //    //{
                //    //    user.FacebookName = authToken.DisplayName;
                //    //    user.FacebookFirstName = authToken.FirstName;
                //    //    user.FacebookLastName = authToken.LastName;
                //    //    user.FacebookEmail = authToken.Email;
                //    //}
                //    //else
                if (authToken.Provider == GoogleOpenIdOAuthProvider.Name)
                {
                    user.GoogleUserId = authToken.UserId;
                    user.GoogleFullName = authToken.FullName;
                    user.GoogleEmail = authToken.Email;
                    if (user.Email == null) user.Email = user.GoogleEmail;
                    if (user.UserName == null) user.UserName = user.GoogleUserId;
                }
                else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
                {
                    user.YahooUserId = authToken.UserId;
                    user.YahooFullName = authToken.FullName;
                    user.YahooEmail = authToken.Email;
                }
            }

            var adminUserNames = AppHost.AppConfig.AdminUserNames;

            if (AppHost.AppConfig.AdminUserNames.Contains(session.UserAuthName)
                && !session.HasRole(RoleNames.Admin))
            {
                using (var assignRoles = authService.ResolveService<AssignRolesService>())
                {
                    assignRoles.Post(new AssignRoles
                        {
                            UserName = session.UserAuthName,
                            Roles = { RoleNames.Admin }
                        });
                    //authService.TryResolve<IDbConnectionFactory>().Run(db => db.Save(user));
                }
            }

            //Resolve the DbFactory from the IOC and persist the user info
        }
Example #5
0
        public bool HasAllRoles(IAuthSession session, IAuthRepository authRepo)
        {
            if (session == null)
            {
                return(false);
            }

            return(this.RequiredRoles.All(x => session.HasRole(x, authRepo)));
        }
Example #6
0
        private static bool SessionHasAllRoles(IRequest req, IAuthSession session, IAuthRepository authRepo, ICollection <string> requiredRoles)
        {
            if (session.HasRole(RoleNames.Admin, authRepo))
            {
                return(true);
            }

            if (requiredRoles.All(x => session.HasRole(x, authRepo)))
            {
                return(true);
            }

            session.UpdateFromUserAuthRepo(req, authRepo);

            if (requiredRoles.All(x => session.HasRole(x, authRepo)))
            {
                req.SaveSession(session);
                return(true);
            }

            return(false);
        }
Example #7
0
        public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary <string, string> authInfo)
        {
            base.OnAuthenticated(authService, session, tokens, authInfo);

            //Populate all matching fields from this session to your own custom User table
            var user = session.ConvertTo <User>();

            user.Id = int.Parse(session.UserAuthId);
            user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()
                ? CreateGravatarUrl(session.Email, 64)
                : null;

            foreach (var authToken in session.ProviderOAuthAccess)
            {
                if (authToken.Provider == FacebookAuthProvider.Name)
                {
                    user.FacebookName      = authToken.DisplayName;
                    user.FacebookFirstName = authToken.FirstName;
                    user.FacebookLastName  = authToken.LastName;
                    user.FacebookEmail     = authToken.Email;
                }
                else if (authToken.Provider == TwitterAuthProvider.Name)
                {
                    user.TwitterName = user.DisplayName = authToken.UserName;
                }
                else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
                {
                    user.YahooUserId   = authToken.UserId;
                    user.YahooFullName = authToken.FullName;
                    user.YahooEmail    = authToken.Email;
                }
            }

            var userAuthRepo = authService.TryResolve <IAuthRepository>();

            if (AppHost.AppConfig.AdminUserNames.Contains(session.UserAuthName) &&
                !session.HasRole(RoleNames.Admin, userAuthRepo))
            {
                var userAuth = userAuthRepo.GetUserAuth(session, tokens);
                userAuthRepo.AssignRoles(userAuth, roles: new[] { RoleNames.Admin });
            }

            //Resolve the DbFactory from the IOC and persist the user info
            using (var db = authService.TryResolve <IDbConnectionFactory>().Open())
            {
                db.Save(user);
            }
        }
Example #8
0
        public static AuthorizationResponse GetAuthorizations(this Authorization request,
                                                              Factory factory, IRequestContext requestContext)
        {
            var          httpRequest = requestContext.Get <IHttpRequest>();
            IAuthSession session     = httpRequest.GetSession();

            if (!session.HasRole(RoleNames.Admin))
            {
                request.UserId = int.Parse(session.UserAuthId);
            }

            List <AuthRole>       roles       = new List <AuthRole>();
            List <AuthPermission> permissions = new List <AuthPermission>();

            List <AuthRoleUser>       aur     = new List <AuthRoleUser>();
            List <AuthRole>           rol     = new List <AuthRole>();
            List <AuthPermission>     per     = new List <AuthPermission>();
            List <AuthRolePermission> rol_per = new List <AuthRolePermission>();

            factory.Execute(proxy =>
            {
                aur = proxy.Get <AuthRoleUser>(q => q.UserId == request.UserId);
                //proxy.GetByUserIdFromCache<AuthRoleUser>(request.UserId); // causa problemas .net !!! no en mono
                rol     = proxy.GetFromCache <AuthRole>();
                per     = proxy.GetFromCache <AuthPermission>();
                rol_per = proxy.GetFromCache <AuthRolePermission>();

                foreach (var r in aur)
                {
                    AuthRole ar = rol.First(x => x.Id == r.AuthRoleId);
                    roles.Add(ar);
                    rol_per.Where(q => q.AuthRoleId == ar.Id).ToList().ForEach(y => {
                        AuthPermission up = per.First(p => p.Id == y.AuthPermissionId);
                        if (permissions.FindIndex(f => f.Name == up.Name) < 0) // .IndexOf(up) <0)
                        {
                            permissions.Add(up);
                        }
                    });
                }
                ;
            });

            return(new AuthorizationResponse()
            {
                Permissions = permissions,
                Roles = roles,
            });
        }
        public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo)
        {
            base.OnAuthenticated(authService, session, tokens, authInfo);

            //Populate all matching fields from this session to your own custom User table
            var user = session.ConvertTo<User>();
            user.Id = int.Parse(session.UserAuthId);
            user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()
                ? CreateGravatarUrl(session.Email, 64)
                : null;

            foreach (var authToken in session.ProviderOAuthAccess)
            {
                if (authToken.Provider == FacebookAuthProvider.Name)
                {
                    user.FacebookName = authToken.DisplayName;
                    user.FacebookFirstName = authToken.FirstName;
                    user.FacebookLastName = authToken.LastName;
                    user.FacebookEmail = authToken.Email;
                }
                else if (authToken.Provider == TwitterAuthProvider.Name)
                {
                    user.TwitterName = user.DisplayName = authToken.UserName;
                }
                else if (authToken.Provider == YahooOpenIdOAuthProvider.Name)
                {
                    user.YahooUserId = authToken.UserId;
                    user.YahooFullName = authToken.FullName;
                    user.YahooEmail = authToken.Email;
                }
            }

            var userAuthRepo = authService.TryResolve<IAuthRepository>();
            if (AppHost.AppConfig.AdminUserNames.Contains(session.UserAuthName)
                && !session.HasRole(RoleNames.Admin, userAuthRepo))
            {
                var userAuth = userAuthRepo.GetUserAuth(session, tokens);
                userAuthRepo.AssignRoles(userAuth, roles: new[] { RoleNames.Admin });
            }

            //Resolve the DbFactory from the IOC and persist the user info
            using (var db = authService.TryResolve<IDbConnectionFactory>().Open())
            {
                db.Save(user);
            }
        }
Example #10
0
    public override void OnAuthenticated(IServiceBase authService,
                                         IAuthSession session,
                                         IAuthTokens tokens,
                                         Dictionary <string, string> authInfo)
    {
        base.OnAuthenticated(authService, session, tokens, authInfo);
        var appSettings         = authService.TryResolve <IAppSettings>();
        var userAuthRepo        = authService.TryResolve <IAuthRepository>();
        var userAuth            = userAuthRepo.GetUserAuth(session, tokens);
        var dbConnectionFactory = authService.TryResolve <IDbConnectionFactory>();

        foreach (var authTokens in session.ProviderOAuthAccess)
        {
            if (authTokens.Provider.ToLower() == "github")
            {
                GithubProfileUrl = session.GetProfileUrl();
            }
            if (authTokens.Provider.ToLower() == "twitter")
            {
                TwitterProfileUrl = session.GetProfileUrl();
                if (appSettings.GetList("TwitterAdmins").Contains(session.UserName) &&
                    !session.HasRole(RoleNames.Admin))
                {
                    userAuthRepo.AssignRoles(userAuth, roles: new[] { RoleNames.Admin });
                }
            }
            DefaultProfileUrl = GithubProfileUrl ?? TwitterProfileUrl;
            using (var db = dbConnectionFactory.OpenDbConnection())
            {
                var userAuthInstance = db.Single <CustomUserAuth>(x =>
                                                                  x.Id == this.UserAuthId.ToInt());
                if (userAuthInstance != null)
                {
                    userAuthInstance.DefaultProfileUrl = this.DefaultProfileUrl;
                    db.Save(userAuthInstance);
                }
            }
        }
    }
Example #11
0
		public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
		{
			base.OnAuthenticated(authService, session, tokens, authInfo);

			//Populate all matching fields from this session to your own custom User table
			var user = session.TranslateTo<User>();
			user.Id = int.Parse(session.UserAuthId);
			user.GravatarImageUrl64 = !session.Email.IsNullOrEmpty()
				? CreateGravatarUrl(session.Email, 64)
				: null;

			foreach (var authToken in session.ProviderOAuthAccess)
			{
				if (authToken.Provider == FacebookAuthProvider.Name)
				{
					user.FacebookName = authToken.DisplayName;
					user.FacebookFirstName = authToken.FirstName;
					user.FacebookLastName = authToken.LastName;
					user.FacebookEmail = authToken.Email;
				}
				else if (authToken.Provider == TwitterAuthProvider.Name)
				{
					user.TwitterName = authToken.DisplayName;
				}
			}

			if (AppHost.Config.AdminUserNames.Contains(session.UserAuthName)
				&& !session.HasRole(RoleNames.Admin))
			{
				var assignRoles = authService.ResolveService<AssignRolesService>();
				assignRoles.Execute(new AssignRoles {
					UserName = session.UserAuthName,
					Roles = { RoleNames.Admin }
				});
			}

			//Resolve the DbFactory from the IOC and persist the user info
			authService.TryResolve<IDbConnectionFactory>().Exec(dbCmd => dbCmd.Save(user));
		}
Example #12
0
        internal static bool HasAnyPermissions(this IAuthSession session, ICollection <string> permissions,
                                               IAuthRepository authRepo, IRequest req)
        {
            var allPerms = session.GetPermissions(authRepo);

            if (permissions.Any(allPerms.Contains))
            {
                return(true);
            }

            if (session.HasRole(RoleNames.Admin, authRepo))
            {
                return(true);
            }

            allPerms = session.GetPermissions(authRepo);
            if (permissions.Any(allPerms.Contains))
            {
                req.SaveSession(session);
                return(true);
            }
            return(false);
        }
 public bool HasAllRoles(IAuthSession session)
 {
     return this.RequiredRoles
         .All(requiredRole => session != null
             && session.HasRole(requiredRole));
 }
Example #14
0
 public bool HasAnyRoles(IAuthSession session)
 {
     return(this.RequiredRoles
            .Any(requiredRole => session != null &&
                 session.HasRole(requiredRole)));
 }
 public virtual bool HasAnyRoles(IAuthSession session, IAuthRepository authRepo)
 {
     return this.RequiredRoles
         .Any(requiredRole => session != null
             && session.HasRole(requiredRole, authRepo));
 }
 public static bool HasAdminRole(this IAuthSession sesion, IAuthRepository userAuthRepo)
 {
     return(sesion.HasRole("Admin", userAuthRepo));
 }
Example #17
0
        public bool IsAuthorized(Operation operation, IRequest req, IAuthSession session)
        {
            if (HostContext.HasValidAuthSecret(req))
            {
                return(true);
            }

            if (operation.RequiresAuthentication && !session.IsAuthenticated)
            {
                return(false);
            }

            var authRepo = HostContext.AppHost.GetAuthRepository(req);

            using (authRepo as IDisposable)
            {
                if (!operation.RequiredRoles.IsEmpty() && !operation.RequiredRoles.All(x => session.HasRole(x, authRepo)))
                {
                    return(false);
                }

                if (!operation.RequiredPermissions.IsEmpty() && !operation.RequiredPermissions.All(x => session.HasPermission(x, authRepo)))
                {
                    return(false);
                }

                if (!operation.RequiresAnyRole.IsEmpty() && !operation.RequiresAnyRole.Any(x => session.HasRole(x, authRepo)))
                {
                    return(false);
                }

                if (!operation.RequiresAnyPermission.IsEmpty() && !operation.RequiresAnyPermission.Any(x => session.HasPermission(x, authRepo)))
                {
                    return(false);
                }

                return(true);
            }
        }
        public bool IsAuthorized(Operation operation, IRequest req, IAuthSession session)
        {
            if (HostContext.HasValidAuthSecret(req))
                return true;

            if (operation.RequiresAuthentication && !session.IsAuthenticated)
                return false;

            var authRepo = HostContext.AppHost.GetAuthRepository(req);
            using (authRepo as IDisposable)
            {
                if (!operation.RequiredRoles.IsEmpty() && !operation.RequiredRoles.All(x => session.HasRole(x, authRepo)))
                    return false;

                if (!operation.RequiredPermissions.IsEmpty() && !operation.RequiredPermissions.All(x => session.HasPermission(x, authRepo)))
                    return false;

                if (!operation.RequiresAnyRole.IsEmpty() && !operation.RequiresAnyRole.Any(x => session.HasRole(x, authRepo)))
                    return false;

                if (!operation.RequiresAnyPermission.IsEmpty() && !operation.RequiresAnyPermission.Any(x => session.HasPermission(x, authRepo)))
                    return false;

                return true;
            }
        }
        public static bool HasAdminRole(this IAuthSession sesion, IRequest peticion)
        {
            var userAuthRepo = peticion.GetAuthRepo();

            return(sesion.HasRole("Admin", userAuthRepo));
        }
Example #20
0
 public virtual bool HasAnyRoles(IAuthSession session, IAuthRepository authRepo)
 {
     return(this.RequiredRoles
            .Any(requiredRole => session != null &&
                 session.HasRole(requiredRole, authRepo)));
 }
 public virtual bool HasAnyRoles(IAuthSession session)
 {
     return this.RequiredRoles
         .Any(requiredRole => session != null
             && session.HasRole(requiredRole));
 }