ufront / ufront-easyauth

Easy database-driven authentication for ufront. Each group is assigned permissions, and each user can belong to certain groups.
MIT License
1 stars 2 forks source link

Extending EasyAuth model. #3

Closed tiagolr closed 9 years ago

tiagolr commented 9 years ago

Is it possible to extend easyAuth model without modifying the lib? For instance adding a "userProfile" relationship to User. What would be the best way to do so? Thanks.

tiagolr commented 9 years ago

@jasononeil any ideas? Thinking about adding a email field so the user can sign in with both username or email, does not seem trivial, at least i have to change most of the easyAuth api so i can use MyExtendedUser.manager instead of User.manager.

Any thoughts on this? If its too complicated i'll skip it, but associating a profile with a user at least is something ill need sooner or later, I'll probably find some way to do it but you probably have some ideas on the subject, thanks a lot.

tiagolr commented 9 years ago

Ok, ended up creating another model object Account, and modifying SignupApi to make use of it, example:

public function createAccount(username:String, password:String, email:String):Account {

        if (User.manager.select( { username:username } ) != null) throw "Username already exists.";
        if (Account.manager.select( { email:email } ) != null) throw "Email already registered.";

        var user = easyAuthApi.createUser(username, password).sure();
        var account = new Account(user, email);

        try account.save()
        catch (e:Dynamic) {
            user.delete();
            throw e;
        }

        return account;
    }

Now tasks and server use the signup api and its working fine so far.

jasononeil commented 9 years ago

Sorry I didn't get to this earlier - I've had an extremely few busy weeks at work.

For the record, I do much the same thing. An example is seen in the modelschool library that provides common database models for a K-12 school setup. Basically I have a "Person" model - which belongs to a "User" - that has information about their name, gender, birthday etc. Further I have "StaffMember" and "Student" models which has even more specific information.

The way Haxe db objects work it seems hard to use Object Oriented extension, rather, using multiple objects that are related to each other, and with a separate table for each, seems the way to go.

If that's not enough, you can always create your own slightly different UFAuthHandler that has one object (and one database table) that includes everything.