Commit fafbc839 authored by Alex Kakiris's avatar Alex Kakiris
Browse files

use real user role for ACL and navigation

parent 698be303
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -8,14 +8,17 @@ export const AppAbility = Ability
 * admin can manage everything and client can just visit ACL page
 */
const defineRulesFor = (role, subject) => {
  const { can, rules } = new AbilityBuilder(AppAbility)
  const { can, cannot, rules } = new AbilityBuilder(AppAbility)
  if (role === 'admin') {
    can('manage', 'all')
    cannot('read', 'user-home')
  } else if (role === 'user') {
    can('read', 'user-home')
  } else if (role === 'client') {
    can(['read'], 'acl-page')
  } else {
    can(['read', 'create', 'update', 'delete'], subject)
  }
  // unknown role gets zero rules, which means no access to anything
  // Add more roles and rules as needed for your application
  
  return rules
}
+6 −15
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@ import authConfig from 'src/configs/auth'
import {
  clearCookies,
  getAccessToken,
  getUserData,
  setAccessToken,
  setRefreshToken,
  setUserData
@@ -69,7 +68,7 @@ const AuthProvider = ({ children }) => {
          
          console.log('After auth/me request')
          console.log('AuthContext response: ', response)
          setUser(getUserData())   // getUserData() -> devuelve el objeto JSON almacenado en la cookie USER_DATA_KEY
          setUser({username: response.data.user, role: response.data.role}) 
          setLoading(false)
        
        // IF TOKEN VALIDATION FAILS,
@@ -81,7 +80,7 @@ const AuthProvider = ({ children }) => {
            const response = await axiosInstance.post(authConfig.refreshEndpoint)
            setAccessToken(response.data.access_token)
            console.log('AuthContext access token: ', response.data.access_token)
            setUser(getUserData())
            setUser({username: response.data.user, role: response.data.role})
            setLoading(false)
          } catch (error) {
            // clearCookies()
@@ -118,24 +117,16 @@ const AuthProvider = ({ children }) => {

      console.log('Login request completed successfully')

      if (params.rememberMe) {
        console.log('Entered params.rememberMe branch')
      setAccessToken(response.data.access_token)
        console.log('AuthContext access token: ', response.data.access_token)
        const pruebatoken = getAccessToken()
      setRefreshToken(response.data.refresh_token)
        console.log
        setUserData({ username: params.username, role: 'admin' })
      }
      setUserData({ username: params.username, role: response.data.role })

      const returnUrl = router.query.returnUrl
      setUser({ username: params.username, role: 'admin' })
      setUser({ username: params.username, role: response.data.role })

      const redirectURL = returnUrl && returnUrl !== '/' ? returnUrl : '/'
      router.replace(redirectURL)

      const prueba = getAccessToken()

    } catch (err) {
      console.log(err)
      if (errorCallback) errorCallback(err)
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
 */
const getHomeRoute = role => {
  if (role === 'client') return '/acl'
  else if (role === 'user') return '/home'
  else return '/home-dashboards'
}

+25 −6
Original line number Diff line number Diff line
const navigation = () => {
  return [
    {
      title: 'Home',
      icon: 'mdi:home-outline',
      path: '/home',
      action: 'read',
      subject: 'user-home'
    },
    {
      title: 'Dashboard',
      icon: 'mdi:home-outline',
      path: '/home-dashboards'
      path: '/home-dashboards',
      action: 'manage',
      subject: 'all'
    },
    {
      title: 'Invokers',
      icon: 'mdi:baseline-people',
      path: '/invokers'
      path: '/invokers',
      action: 'manage',
      subject: 'all'
    },
    {
      title: 'Providers',
      icon: 'mdi:baseline-people',
      path: '/providers'
      path: '/providers',
      action: 'manage',
      subject: 'all'
    },
    {
      title: 'APIs',
      icon: 'mdi:baseline-people',
      path: '/services'
      path: '/services',
      action: 'manage',
      subject: 'all'
    },
    {
      title: 'Users',
      icon: 'mdi:baseline-people',
      path: '/users'
      path: '/users',
      action: 'manage',
      subject: 'all'
    },
    {
      title: 'Configuration',
      icon: 'mdi:baseline-people',
      path: '/configuration'
      path: '/configuration',
      action: 'manage',
      subject: 'all'
    }
    
    // ,
+18 −0
Original line number Diff line number Diff line
import Box from '@mui/material/Box'
import Typography from '@mui/material/Typography'

const Home = () => {
    return (
        <Box sx={{ p:6 }}>
            <Typography variant="h4">Welcome to the Home Page</Typography>
            <Typography sx={{ mt: 2 }}>You are logged in.</Typography>
        </Box>
    )
}

Home.acl = {
    action: 'read',
    subject: 'user-home'
}

export default Home
 No newline at end of file