# Manage users through a web service

**URL:** <https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513>\
**Category:** Rails\
**Created:** [December 28, 2006, 2:11am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513 "2006-12-28T02:11:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [December 28, 2006, 2:11am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513/1 "2006-12-28T02:11:59Z")

</div>

First of all, I’ll apologize because this is going to be more of a  
general setup question rather than a specific coding question. I’m  
relatively new to Rails and I’ve never built a web service before. So,  
here’s my problem:

I’m building 3 new applications that all relate to one another, but can  
also be stand-alone. Meaning a user can register with each one  
individually. However, one of the requirements is to only have one  
login. So once a user registers with any of the 3 applications, they  
are then in the system and can use the same username/password they’ve  
registered.

Now, it seems to me that this is a perfect situation to have a web  
service to manage users. If not, you can stop here and possibly explain  
a better solution? (Note: I don’t want to use openID).

Here are the things I would want to do in the web service:

1. Register a user, saving all information to the webservice (but only  
email and password will be required).
2. Login a user, returning their user\_id to be held in session. This  
would require a call to the webservice to verify that the user exists,  
and if not, prompt them to register.

Those are the absolute minimum requirements. Now, what else should I  
have? I want to keep track of last logins and login attempts as well,  
so I was thinking about having the following tables: Users, Audits,  
Logs. Users is obvious, Audits would keep track of logins and attempts,  
Logs would record eroneous errors (or should I use a flat file for  
that?).

If you’ve made it this far, and that sounds reasonable, what type of  
code needs to be written to connect to the webservice? I’m not  
completely sure I know how it works. Would I have a method like  
“verify\_user(email, pwd)” that would return xml data if the user exists,  
and an error code if they don’t?

I’ll stop here for now, and ask more coding-type questions once I figure  
out the general idea of how to accomplish this. If someone can explain  
the basics of an implementation to have one way to track multiple users  
across 3 applications, I’m open to suggestions.

Again, I’m sorry this is kind of general, but I didn’t know which forum  
would be most appropriate. Thanks for any help on this matter.

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [December 28, 2006, 2:24am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513/2 "2006-12-28T02:24:24Z")

</div>

Hi~

On Dec 27, 2006, at 5:11 PM, rph wrote:

> individually. However, one of the requirements is to only have one
> 
> attempts,  
> I’ll stop here for now, and ask more coding-type questions once I  
> figure  
> out the general idea of how to accomplish this. If someone can  
> explain  
> the basics of an implementation to have one way to track multiple  
> users  
> across 3 applications, I’m open to suggestions.
> 
> Again, I’m sorry this is kind of general, but I didn’t know which  
> forum  
> would be most appropriate. Thanks for any help on this matter.

```
While you could do this with a webservice there may exist an easier

```

solution. If you control all three of these rails apps that need to  
work together then you may want to consider letting all 3 apps use  
the same database for at least the users table. That is probably the  
simplest way to share users between apps.

```
I'll let someone else chime in about a webservice approach but it

```

feels like a little bit of over kill to me for just sharing users.

Cheers  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [December 28, 2006, 2:57am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513/3 "2006-12-28T02:57:30Z")

</div>

Ezra Z. wrote:

> Hi~
> 
> On Dec 27, 2006, at 5:11 PM, rph wrote:
> 
> > individually. However, one of the requirements is to only have one
> > 
> > attempts,  
> > I’ll stop here for now, and ask more coding-type questions once I  
> > figure  
> > out the general idea of how to accomplish this. If someone can  
> > explain  
> > the basics of an implementation to have one way to track multiple  
> > users  
> > across 3 applications, I’m open to suggestions.
> > 
> > Again, I’m sorry this is kind of general, but I didn’t know which  
> > forum  
> > would be most appropriate. Thanks for any help on this matter.
> 
> While you could do this with a webservice there may exist an easier  
> solution. If you control all three of these rails apps that need to  
> work together then you may want to consider letting all 3 apps use  
> the same database for at least the users table. That is probably the  
> simplest way to share users between apps.
> 
> I’ll let someone else chime in about a webservice approach but it  
> feels like a little bit of over kill to me for just sharing users.
> 
> Cheers  
> – Ezra Z.  
> – Lead Rails Evangelist  
> – removed\_email\_address@domain.invalid  
> – Engine Y., Serious Rails Hosting  
> – (866) 518-YARD (9273)

Thank you… excellent point. How would that work, though? If I had a  
database for the users part of the applications (users\_production), and  
the next application had its own database (app1\_production), how would I  
connect to the users\_production database to let the app1 application use  
the users table? I originally thought about that, but thought that it  
would make more sense to keep them separate, but I completely see your  
point. I just don’t know how to do it. Any thoughts on this? Or other  
thoughts? Thanks again.

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [December 28, 2006, 3:20am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513/4 "2006-12-28T03:20:37Z")

</div>

Hi, I short you will need a database or other persistence for holding  
the users’ ids and password for validating against.

Next, you can create your webservice in the language of your choice  
but I will use ruby in my explaination. This webservice will either  
communicate to your persistent entities using Rails AR or the Ruby  
gdbm module.

Now, you will create a standalone SOAP server that regsiters your  
webservice. See Page 249 of “Programming Ruby” for more detail.

Finally, you should be able to refactor existing authetication schemes  
to easily use your web service.

Good luck,

-Cconrad

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [December 28, 2006, 3:28am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513/5 "2006-12-28T03:28:38Z")

</div>

Hi, there maybe few glitches in the scheme but I’m sure that you can  
figure it out and they should by obvious to you. I must get back to  
shopping with my girl friend and I wish you all the best.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [December 28, 2006, 4:10am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513/6 "2006-12-28T04:10:46Z")

</div>

rph wrote:

> > > figure
> > 
> > – Ezra Z.  
> > thought that it would make more sense to keep them separate, but I  
> > completely see your point. I just don’t know how to do it. Any  
> > thoughts on this? Or other thoughts? Thanks again.

Its relatively simple:

In database.yml file of app1\_production:

users:  
adapter: mysql  
database: original\_users  
host: localhost  
username: connector  
password: wtf

So, in the model of same app, you would say:

class User \< ActiveRecord::Base  
if RAILS[‘env’] == ‘production’  
establish\_connections :users  
else  
# normal test thingy  
end

# rest of the model code comes here unhindered.

end

---

<div class="post-metadata">

**Author:** ![rph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rph](https://www.ruby-forum.com/u/rph)\
**Post date:** [December 28, 2006, 4:25am UTC](https://www.ruby-forum.com/t/manage-users-through-a-web-service/80513/7 "2006-12-28T04:25:45Z")

</div>

Hemant K. wrote:

> rph wrote:
> 
> > > > figure
> > > 
> > > – Ezra Z.  
> > > thought that it would make more sense to keep them separate, but I  
> > > completely see your point. I just don’t know how to do it. Any  
> > > thoughts on this? Or other thoughts? Thanks again.
> 
> Its relatively simple:
> 
> In database.yml file of app1\_production:
> 
> users:  
> adapter: mysql  
> database: original\_users  
> host: localhost  
> username: connector  
> password: wtf
> 
> So, in the model of same app, you would say:
> 
> class User \< ActiveRecord::Base  
> if RAILS[‘env’] == ‘production’  
> establish\_connections :users  
> else  
> # normal test thingy  
> end
> 
> # rest of the model code comes here unhindered.
> 
> end

Ok, that seems simple enough. So when you call establish\_connections  
:users does that allow the other database (app1\_development) to have its  
connection as well? Basically, is the users definition in the  
database.yml file an additional connection criteria along side of  
app1\_production? So the default for my other models would still be to  
use app1\_production, while I’m specifically telling my User model to use  
the :users connection? Sorry for asking for additional explanation.  
Thanks a lot for your help, I think this is the way to go for what I  
need to do.
