1

I am working on developing an application for multiple companies.

I am hitting a wall on how to handle the user roles from within each company. I have seen user roles for various other applications, but none of them quite fit my scenario.

Companies
 - Company A {
      users {
         admins {
            admin1:{}
         },
         staff {
            staff1:{}
         },
         drivers {
            driver1:{}
         }
      }
   },
 - Company B {
     ...
   }

For each company... - I want admins of a company to create other admins/staff/drivers - I want staff of a company to create other drivers - I want drivers to be the lowest, not able to create any users

I see no good way of accomplishing this task! Any help would be greatly appreciated!

Thanks!

Connor

Connor
  • 97
  • 10

1 Answers1

0

It is recommended to flatten and denormalise your data, so your database structure could be more similar to:

{
  "companies": {
    "company1": {
      "name": "Company A",
      "users": {
        "user1": {
            "admin": true
        }
      }
    },
    "company2": {
      "name": "Company B",
      "users": {
        "user1": {
            "admin": true
        },
        "user2": {
            "driver": true
        }
      }
    },
    "company3": { ... }
  },

  "users": {
    "user1": {
      "companies": {
        "company1": {
            "admin": true
        },
        "company2": {
            "admin": true
        }
      }
    },
    "user2": {
      "companies": {
        "company2": {
            "driver": true
        }
      }
    },
    "user3": { ... }
  }
}

This pattern is called denormalization and is the suggested approach when using NoSQL databases (like Firebase Realtime Database). The main downside to this is data duplication, but for details on dealing with this, see this answer which explains a number of methods (although the examples are in JavaScript, the premise still applies).

Using this structure makes it easy to associate a user with multiple companies, while also easily identifying which users are associated with each company, and what role they have within that company.

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
  • Thanks for the tip, while I find this extremely helpful in structuring my database, it doesn't solve the issue of having admins creating accounts for staff and drivers etc... – Connor Oct 20 '17 at 14:50
  • What method do you need the admins to create accounts for others? Do you mean creating Firebase Authentication accounts or by simply adding data to the database to represent users? – Grimthorr Oct 20 '17 at 15:03
  • The staff manage what the drivers have to do, but they also need to be able to create new drivers. The drivers need to have some sort of authentication process on a mobile application so that I can track what the do. – Connor Oct 20 '17 at 15:20
  • You can use the [Firebase Authentication](https://firebase.google.com/docs/auth/) package to manage authentication and accounts. For different permissions or roles, this is up to you to manage as there isn't any built in functionality, but using the database as above is a good way to store user profile data & roles, so you could use this as a basis. Also see [this answer](https://stackoverflow.com/a/38666983) which uses this method to identify which role a user has, and then take a different action depending on what they are allowed to do. – Grimthorr Oct 21 '17 at 16:41